-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (48 loc) · 1.59 KB
/
server.js
File metadata and controls
60 lines (48 loc) · 1.59 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
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const fs = require('fs');
const path = require('path');
// Default port
const port = parseInt(process.env.PORT || '4200', 10);
// Determine if we should bind to all interfaces or just localhost
let networkMode = false;
// Override with environment variable if set
if (process.env.FLUJO_NETWORK_MODE === '1' || process.env.FLUJO_NETWORK_MODE === 'true') {
networkMode = true;
}
// Host to bind to
const hostname = networkMode ? '0.0.0.0' : 'localhost';
// Prepare the Next.js app
const app = next({
dev: process.env.NODE_ENV !== 'production',
hostname,
port,
});
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(port, hostname, (err) => {
if (err) throw err;
const addressInfo = networkMode ?
`all interfaces (${hostname}) on port ${port}` :
`${hostname}:${port}`;
console.log(`> Ready on ${addressInfo}`);
if (networkMode) {
// Log the actual IP addresses for network access
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
console.log('> Available on:');
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip internal and non-IPv4 addresses
if (net.family === 'IPv4' && !net.internal) {
console.log(`> http://${net.address}:${port}`);
}
}
}
}
});
});