-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (55 loc) · 1.66 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
57
58
59
60
61
62
63
64
65
66
import express from "express";
import http from "node:http";
import { createBareServer } from "@tomphttp/bare-server-node";
import cors from "cors";
import path from "path";
import { hostname } from "node:os";
const server = http.createServer();
const app = express(server);
const __dirname = process.cwd();
const bareServer = createBareServer("/b/");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files from the React build directory
const buildPath = path.join(__dirname, "dist"); // Update if your build directory is different
app.use(express.static(buildPath));
app.use(cors());
server.on("request", (req, res) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeRequest(req, res);
} else {
app(req, res);
}
});
server.on("upgrade", (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head);
} else {
socket.end();
}
});
// Handle any other routes with the React app
app.get("*", (req, res) => {
res.sendFile(path.join(buildPath, "index.html"));
});
const PORT = 3000;
server.on("listening", () => {
const address = server.address();
console.log("Listening on:");
console.log(`\thttp://localhost:${address.port}`);
console.log(`\thttp://${hostname()}:${address.port}`);
console.log(
`\thttp://${
address.family === "IPv6" ? `[${address.address}]` : address.address
}:${address.port}`
);
});
server.listen({ port: PORT });
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
function shutdown() {
console.log("SIGTERM signal received: closing HTTP server");
server.close();
bareServer.close();
process.exit(0);
}