-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
63 lines (55 loc) · 1.58 KB
/
app.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import createBareServer from '@tomphttp/bare-server-node';
import http from 'http';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import serveStatic from 'serve-static';
import * as custombare from './static/customBare.mjs';
const PORT = process.env.PORT || 3000;
const bareServer = createBareServer('/bare/', {
logErrors: false,
localAddress: undefined
});
const serve = serveStatic(join(
dirname(fileURLToPath(import.meta.url)),
'static/'
), {
fallthrough: false,
maxAge: 5 * 60 * 1000
});
const server = http.createServer();
server.on('request', (request, response) => {
try {
if (custombare.route(request, response)) return true;
if (bareServer.shouldRoute(request)) {
bareServer.routeRequest(request, response);
} else {
serve(request, response, err => {
response.writeHead(err?.statusCode || 500, null, {
"Content-Type": "text/plain"
})
response.end(err?.stack)
});
}
} catch (e) {
response.writeHead(500, "Internal Server Error", {
"Content-Type": "text/plain"
})
response.end(e.stack)
}
});
server.on('upgrade', (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.listen(PORT);
if (process.env.UNSAFE_CONTINUE)
process.on("uncaughtException", (err, origin) => {
console.error(`Critical error (${origin}):`)
console.error(err)
console.error("UNSAFELY CONTINUING EXECUTION")
console.error()
})
console.log(`Server running at http://localhost:${PORT}/.`);