forked from TerbiumOS/webOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 984 Bytes
/
index.js
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
39
40
41
42
import createBareServer from "@tomphttp/bare-server-node";
import http from "node:http";
import { fileURLToPath } from 'node:url';
import serveStatic from 'serve-static';
const httpServer = http.createServer();
const serve = serveStatic(
fileURLToPath(new URL('./static/', import.meta.url)), {
fallthrough: false,
}
);
const bareServer = createBareServer('/bare/');
httpServer.on('request', (req, res) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeRequest(req, res);
} else {
serve(req, res, (err) => {
res.writeHead(err?.statusCode || 500, {
'Content-Type': 'text/plain',
});
res.end(err?.stack);
});
}
});
httpServer.on('upgrade', (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
httpServer.on('listening', () => {
console.log('HTTP server listening');
});
httpServer.listen({
port: process.env.PORT || 6969,
});