-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
47 lines (40 loc) · 1.2 KB
/
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
39
40
41
42
43
44
45
46
47
import createBareServer from '@tomphttp/bare-server-node';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import serveStatic from 'serve-static';
const httpServer = createServer();
const bareServer = createBareServer('/bare/');
// The static root is usually relative to the main script in projects that use the Bare server.
// ie. if static.js is at /src/static.js, public will be /public/
// ideally, you will point the public directory relative to the current working directory
// serveStatic('./public/')
// This would ignore the relative location of static.js
const serve = serveStatic(
fileURLToPath(new URL('static/', import.meta.url)),
{
fallthrough: false,
}
);
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.listen({
// port: 8080,
// });
httpServer.listen(8080);