-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (33 loc) · 1.35 KB
/
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
import createServer from '@tomphttp/bare-server-node';
import { fileURLToPath } from "url";
import http from 'http';
import serveStatic from "serve-static";
// The following message MAY NOT be removed
console.log("Incognito\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to redistribute it\nunder the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nYou should have received a copy of the GNU General Public License\nalong with this program. If not, see <https://www.gnu.org/licenses/>.\n")
const port = process.env.PORT || 8080;
const bare = createServer('/bare/');
const serve = serveStatic(fileURLToPath(new URL("./static/", import.meta.url)), { fallthrough: false });
const server = http.createServer();
server.on('request', (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else {
serve(req, res, (err) => {
res.writeHead(err?.statusCode || 500, null, {
"Content-Type": "text/plain",
});
res.end(err?.stack);
});
}
});
server.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req, socket, head)) {
bare.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.listen({
port: port,
});
console.log("Server running on port " + port)