| 1234567891011121314151617181920212223242526272829303132333435363738 |
- import { createServer } from 'node:http';
- import { readFile, stat } from 'node:fs/promises';
- import path from 'node:path';
- import { fileURLToPath } from 'node:url';
- const root = path.dirname(fileURLToPath(import.meta.url));
- const port = Number(process.env.PORT || 4176);
- const mimeTypes = {
- '.html': 'text/html; charset=utf-8',
- '.css': 'text/css; charset=utf-8',
- '.js': 'text/javascript; charset=utf-8',
- '.mjs': 'text/javascript; charset=utf-8',
- '.png': 'image/png',
- '.md': 'text/plain; charset=utf-8',
- };
- createServer(async (request, response) => {
- try {
- const pathname = decodeURIComponent(new URL(request.url, 'http://localhost').pathname);
- const filename = path.resolve(root, `.${pathname === '/' ? '/index.html' : pathname}`);
- if (!filename.startsWith(root + path.sep)) {
- response.writeHead(403).end('Forbidden');
- return;
- }
- if (!(await stat(filename)).isFile()) throw new Error('Not a file');
- const content = await readFile(filename);
- response.writeHead(200, {
- 'Content-Type': mimeTypes[path.extname(filename)] || 'application/octet-stream',
- 'Cache-Control': 'no-store',
- 'X-Content-Type-Options': 'nosniff',
- });
- response.end(content);
- } catch {
- response.writeHead(404).end('Not found');
- }
- }).listen(port, '127.0.0.1', () => {
- console.log(`Prototype: http://127.0.0.1:${port}/#/chat/c1`);
- });
|