This repository was archived by the owner on Apr 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (45 loc) · 1.49 KB
/
server.js
File metadata and controls
52 lines (45 loc) · 1.49 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
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const hostname = '0.0.0.0';
const port = parseInt(process.env.PORT) || 3000; // Use PORT env var from Docker, fallback to 3000
// Set up Next.js options
const nextConfig = {
dev,
hostname,
port
};
// Initialize Next.js
const app = next(nextConfig);
const handle = app.getRequestHandler();
app.prepare().then(async () => {
const server = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
await handle(req, res, parsedUrl);
} catch (err) {
console.error('Error occurred handling', req.url, err);
res.statusCode = 500;
res.end('Internal Server Error');
}
});
// Set a short timeout to fail fast rather than waiting
server.setTimeout(0);
// Handle error during server listen
server.on('error', (e) => {
if (e.code === 'EADDRINUSE') {
console.error(`\x1b[31mError: Port ${port} is already in use.\x1b[0m`);
console.error('Please ensure no other processes are using this port before starting the application.');
process.exit(1);
} else {
console.error('Server error:', e);
process.exit(1);
}
});
server.listen(port, hostname, async (err) => {
if (err) throw err;
const address = server.address();
console.log(`> Ready on http://${hostname === '0.0.0.0' ? 'localhost' : hostname}:${address.port}`);
});
});