forked from Yeti1o1/Pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.62 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
53
54
55
56
// Import express, http, and wisp
import http from 'node:http';
import express from 'express';
import wisp from 'wisp-server-node';
import path from 'node:path';
import { fileURLToPath } from 'url';
import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
// Get current file path and directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Create the express "app"
const app = express();
// Create an HTTP server
const httpServer = http.createServer();
// Define the port to listen on
const port = 8080;
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, "public")));
// Serve UV, baremux, and epoxy files
app.use("/uv/", express.static(uvPath));
app.use("/baremux/", express.static(baremuxPath));
app.use("/epoxy/", express.static(epoxyPath));
// Listen for requests on the HTTP server
httpServer.on('request', (req, res) => {
// Make express handle all of the requests
app(req, res);
});
// Listen for WebSocket upgrades on the HTTP server
httpServer.on('upgrade', (req, socket, head) => {
if (req.url.endsWith('/wisp/')) {
// Route the request to the wisp server if the URL ends in /wisp/
wisp.routeRequest(req, socket, head);
} else {
socket.end();
}
});
// When the server is ready, log that it is ready
httpServer.on('listening', () => {
console.log(`Server listening on http://localhost:${port}`);
});
// Start the HTTP server
httpServer.listen({
port: port
});