-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathserver.ts
More file actions
61 lines (53 loc) · 1.64 KB
/
Copy pathserver.ts
File metadata and controls
61 lines (53 loc) · 1.64 KB
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
import * as http from "node:http";
import { createRequestListener } from "remix/node-fetch-server";
import { router } from "./app/router.ts";
import { assetServer } from "./app/utils/assets.server.ts";
const port = Number(process.env.PORT ?? 3000);
if (!Number.isFinite(port) || port <= 0) {
throw new Error(
`Invalid PORT value "${process.env.PORT ?? ""}". Expected a positive number.`,
);
}
const server = http.createServer(
createRequestListener(async (request) => {
try {
return await router.fetch(request);
} catch (error) {
console.error(error);
return new Response("Internal Server Error", { status: 500 });
}
}),
);
server.listen(port, () => {
console.log(`Server listening on port ${port} (http://localhost:${port})`);
});
installShutdownHandlers(server);
function installShutdownHandlers(server: http.Server) {
let shuttingDown = false;
const shutdown = async (signal: NodeJS.Signals) => {
if (shuttingDown) return;
shuttingDown = true;
console.log(`${signal} received, shutting down HTTP server...`);
const forceExitTimer = setTimeout(() => {
console.error("Timed out waiting for HTTP server to close");
process.exit(1);
}, 10_000);
forceExitTimer.unref();
await assetServer.close();
server.close((error) => {
clearTimeout(forceExitTimer);
if (error) {
console.error("Error while closing HTTP server", error);
process.exit(1);
}
process.exit(0);
});
server.closeAllConnections();
};
process.once("SIGINT", () => {
void shutdown("SIGINT");
});
process.once("SIGTERM", () => {
void shutdown("SIGTERM");
});
}