Spaces:
Sleeping
Sleeping
| import {createServer} from 'node:http' | |
| const config = {host: '0.0.0.0', port: 7860} | |
| const h_cors = { | |
| 'Access-Control-Max-Age': '86400', | |
| 'Access-Control-Allow-Private-Network': 'true', | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Access-Control-Allow-Methods': 'GET, OPTIONS', | |
| 'Access-Control-Allow-Headers': '*', | |
| // 'Access-Control-Allow-Credentials': 'false', // omit this header to disallow | |
| } | |
| const h_all = { | |
| 'Content-Type': 'text/javascript', | |
| 'Cache-Control': 'no-cache', | |
| ...h_cors | |
| } | |
| /** @type {Record<string,((request: import('node:http').ServerResponse) => void)>} */ const resp = { | |
| GET(rx) { rx.writeHead(200, h_all); rx.write('Hello world') }, | |
| OPTIONS(rx) { rx.writeHead(204, h_cors) }, | |
| } | |
| const server = createServer((rq, rx) => { | |
| const handler = resp[rq.method ?? ''] | |
| handler !== undefined && (new URL(`http://${config.host}:${config.port}${rq.url}`)).pathname === '/' ? handler(rx) : rx.writeHead(400) | |
| rx.end(() => void console.log('%s %d %s %s', (new Date()).toISOString(), rx.statusCode, rq.method, rq.url)) | |
| }) | |
| server.listen(config.port, config.host, () => void console.log(`Server started at http://${config.host}:${config.port}`)) | |