Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
22 lines (22 loc) · 1.45 KB
/
Copy pathserver.js
File metadata and controls
22 lines (22 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import { extname, resolve, sep } from 'node:path';
import { fileURLToPath } from 'node:url';
import { handleApi } from './server/api.js';
const root = fileURLToPath(new URL('./public/', import.meta.url));
const types = { '.html': 'text/html; charset=utf-8', '.css': 'text/css', '.js': 'text/javascript', '.json': 'application/json', '.webmanifest': 'application/manifest+json', '.svg': 'image/svg+xml', '.png': 'image/png', '.apk': 'application/vnd.android.package-archive', '.zip': 'application/zip' };
export const server = createServer(async (req, res) => {
const url = new URL(req.url, 'http://localhost');
if (url.pathname.startsWith('/api/')) return handleApi(req, res);
try {
const pathname = decodeURIComponent(url.pathname);
const file = resolve(root, '.' + (pathname === '/' ? '/index.html' : pathname));
if (!file.startsWith(resolve(root) + sep)) throw new Error('invalid path');
const body = await readFile(file);
res.writeHead(200, { 'content-type': types[extname(file)] || 'application/octet-stream', 'x-content-type-options': 'nosniff' });
res.end(body);
} catch { res.writeHead(404); res.end('Nicht gefunden'); }
});
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
server.listen(Number(process.env.PORT || 8787), '127.0.0.1', () => console.log(`Checkit: http://127.0.0.1:${process.env.PORT || 8787}`));
}