-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev-https-server.js
More file actions
70 lines (60 loc) · 2.07 KB
/
dev-https-server.js
File metadata and controls
70 lines (60 loc) · 2.07 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
62
63
64
65
66
67
68
69
70
const { createServer } = require('https');
const { parse } = require('url');
const next = require('next');
const fs = require('fs');
const path = require('path');
const os = require('os');
console.log('Starting Next.js app in dev mode...');
const dev = true; // Set to true for development mode
const app = next({ dev });
const handle = app.getRequestHandler();
console.log('Reading certificate files...');
const certPath = path.join(__dirname, 'certificates', 'dev.cert');
const keyPath = path.join(__dirname, 'certificates', 'dev.key');
console.log('Certificate path:', certPath);
console.log('Key path:', keyPath);
// Function to get local IP address
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const interfaceName in interfaces) {
const networkInterface = interfaces[interfaceName];
for (const network of networkInterface) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (network.family === 'IPv4' && !network.internal) {
return network.address;
}
}
}
return 'localhost';
}
try {
const httpsOptions = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
};
console.log('Certificates loaded successfully');
app
.prepare()
.then(() => {
console.log('Next.js app prepared, creating HTTPS server...');
const localIP = getLocalIP();
createServer(httpsOptions, (req, res) => {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
}).listen(3001, '0.0.0.0', (err) => {
// Changed: bind to all interfaces with '0.0.0.0'
if (err) {
console.error('Error starting server:', err);
throw err;
}
console.log('> Ready on https://localhost:3001');
console.log(`> Also available on your network at https://${localIP}:3001`);
console.log('> For mobile testing, use the network URL');
});
})
.catch((err) => {
console.error('Error preparing Next.js app:', err);
});
} catch (error) {
console.error('Error setting up HTTPS server:', error);
}