1
0

Use nginx to serve the website
All checks were successful
Build and test project / build-and-test (push) Successful in 1m29s

This commit is contained in:
2026-03-19 19:56:24 +01:00
parent 48ea2c183b
commit 347b4f654a
7 changed files with 96 additions and 11 deletions

40
web/serve.mjs Normal file
View File

@@ -0,0 +1,40 @@
import { createServer } from 'node:http';
import { createReadStream, statSync, existsSync } from 'node:fs';
import { join, extname } from 'node:path';
const DIST = join(import.meta.dirname, 'dist');
const PORT = process.env.PORT || 8888;
const MIME = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.wasm': 'application/wasm',
'.zip': 'application/zip',
'.tgz': 'application/gzip',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.webmanifest': 'application/manifest+json',
};
createServer((req, res) => {
const url = new URL(req.url, `http://localhost`);
let filePath = join(DIST, decodeURIComponent(url.pathname));
if (filePath.endsWith('/')) filePath = join(filePath, 'index.html');
if (!extname(filePath) && existsSync(filePath + '/index.html')) filePath += '/index.html';
try {
const stat = statSync(filePath);
if (!stat.isFile()) throw new Error();
res.writeHead(200, { 'Content-Type': MIME[extname(filePath)] || 'application/octet-stream' });
createReadStream(filePath).pipe(res);
} catch {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
}).listen(PORT, () => {
console.log(`Serving web/dist on http://localhost:${PORT}`);
});