-
Notifications
You must be signed in to change notification settings - Fork 248
/
index.js
52 lines (46 loc) · 1.5 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
41
42
43
44
45
46
47
48
49
50
51
52
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';
import { hostname } from 'node:os';
import { createServer } from 'node:http';
import createBareServer from '@tomphttp/bare-server-node';
import express from 'express';
// "@titaniumnetwork-dev/ultraviolet": "^1.0.5",
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
const publicPath = fileURLToPath(new URL('./static/', import.meta.url));
const bare = createBareServer('/bare/');
const server = createServer();
const app = express();
/*
Trust proxy by default, if you are self hosting and not behind a reverse proxy,
you should disable this.
*/
app.set('trust proxy', true);
let dataScript;
const port = process.env.PORT || 3000;
app.use(express.static(publicPath));
app.use('/uv/', express.static(uvPath));
app.use((req, res) => res.status(404).sendFile(join(publicPath, '404.html')));
server.on('request', (req, res) => {
if (bare.shouldRoute(req)) {
bare.routeRequest(req, res);
} else {
app(req, res);
}
});
server.on('upgrade', (req, socket, head) => {
if (bare.shouldRoute(req)) {
bare.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
server.listen({ port }, () => {
console.log('Listening on:');
console.log(`\thttp://localhost:${server.address().port}`);
console.log(`\thttp://${hostname()}:${server.address().port}`);
console.log(
`\thttp://${
server.address().family === 'IPv6' ? `[${server.address().address}]` : server.address().address
}:${server.address().port}`
);
});