-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathindex.mjs
38 lines (34 loc) · 840 Bytes
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createBareServer } from '@tomphttp/bare-server-node';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import serveStatic from 'serve-static';
const bare = createBareServer('/bare/');
const serve = serveStatic(
fileURLToPath(new URL('static/', import.meta.url)),
{
fallthrough: false,
}
);
const server = createServer();
server.on('request', (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
return;
}
serve(req, res, (err) => {
res.writeHead(err?.statusCode || 500, {
'Content-Type': 'text/plain',
});
res.end("Error.");
});
});
server.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req, socket, head)) {
bare.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.listen({
port: process.env.PORT || 8080,
});