-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
45 lines (38 loc) · 1.4 KB
/
server.ts
File metadata and controls
45 lines (38 loc) · 1.4 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
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { initWebSocketServer } from "./lib/websocket";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
void app
.prepare()
.then(() => {
const server = createServer((req, res) => {
// Fix for no-non-null-assertion: Add check for req.url
const parsedUrl = req.url ? parse(req.url, true) : null;
// Handle case when parsedUrl is null
if (!parsedUrl) {
res.writeHead(400);
res.end("Bad Request: Missing URL");
return;
}
// Let the WebSocket server handle WebSocket requests
if (parsedUrl.pathname?.startsWith("/ws")) {
res.writeHead(426);
res.end();
return;
}
void handle(req, res, parsedUrl);
});
// Initialize WebSocket server
const _wss = initWebSocketServer(server);
// Fix for no-floating-promises: Add void operator to indicate promise is intentionally not awaited
void server.listen(3000, () => {
console.log("> Ready on http://localhost:3000");
});
})
.catch((error: unknown) => {
console.error("Error preparing Next.js app:", error);
process.exit(1);
});