|
| 1 | +import { createServer } from 'node:http'; |
| 2 | +import { createServer as createHttpsServer } from 'node:https'; |
| 3 | +import { readFileSync } from 'node:fs'; |
| 4 | +import { networkInterfaces } from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { fileURLToPath, parse } from 'node:url'; |
| 7 | +import next from 'next'; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | + |
| 12 | +const dev = process.env.NODE_ENV !== 'production'; |
| 13 | +const hostname = process.env.HOSTNAME || '0.0.0.0'; |
| 14 | +const port = Number.parseInt(process.env.PORT ?? '3000', 10) || 3000; |
| 15 | +const useHttps = process.env.HTTPS === 'true'; |
| 16 | + |
| 17 | +const app = next({ dev, hostname, port }); |
| 18 | +const handle = app.getRequestHandler(); |
| 19 | + |
| 20 | +function getLocalIP() { |
| 21 | + const interfaces = networkInterfaces(); |
| 22 | + |
| 23 | + const priorityPrefixes = [ |
| 24 | + 'wlp', |
| 25 | + 'wlan', |
| 26 | + 'wlx', |
| 27 | + 'enp', |
| 28 | + 'eth', |
| 29 | + 'ens', |
| 30 | + ]; |
| 31 | + |
| 32 | + const skipPrefixes = ['br-', 'docker', 'veth', 'virbr', 'lo']; |
| 33 | + |
| 34 | + for (const prefix of priorityPrefixes) { |
| 35 | + for (const [name, entries] of Object.entries(interfaces)) { |
| 36 | + if (!name.startsWith(prefix) || !entries) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + for (const iface of entries) { |
| 41 | + if (iface.family === 'IPv4' && !iface.internal) { |
| 42 | + return iface.address; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + for (const [name, entries] of Object.entries(interfaces)) { |
| 49 | + if (!entries || skipPrefixes.some((prefix) => name.startsWith(prefix))) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + for (const iface of entries) { |
| 54 | + if (iface.family === 'IPv4' && !iface.internal) { |
| 55 | + return iface.address; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + for (const entries of Object.values(interfaces)) { |
| 61 | + if (!entries) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + for (const iface of entries) { |
| 66 | + if (iface.family === 'IPv4' && !iface.internal) { |
| 67 | + return iface.address; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return '127.0.0.1'; |
| 73 | +} |
| 74 | + |
| 75 | +async function main() { |
| 76 | + await app.prepare(); |
| 77 | + |
| 78 | + const server = useHttps |
| 79 | + ? createHttpsServer( |
| 80 | + { |
| 81 | + key: readFileSync( |
| 82 | + path.join(__dirname, 'certificates', 'localhost-key.pem') |
| 83 | + ), |
| 84 | + cert: readFileSync( |
| 85 | + path.join(__dirname, 'certificates', 'localhost.pem') |
| 86 | + ), |
| 87 | + }, |
| 88 | + async (req, res) => { |
| 89 | + try { |
| 90 | + const parsedUrl = parse(req.url ?? '/', true); |
| 91 | + await handle(req, res, parsedUrl); |
| 92 | + } catch (error) { |
| 93 | + console.error('Error occurred handling', req.url, error); |
| 94 | + res.statusCode = 500; |
| 95 | + res.end('internal server error'); |
| 96 | + } |
| 97 | + } |
| 98 | + ) |
| 99 | + : createServer(async (req, res) => { |
| 100 | + try { |
| 101 | + const parsedUrl = parse(req.url ?? '/', true); |
| 102 | + await handle(req, res, parsedUrl); |
| 103 | + } catch (error) { |
| 104 | + console.error('Error occurred handling', req.url, error); |
| 105 | + res.statusCode = 500; |
| 106 | + res.end('internal server error'); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + server.listen(port, hostname, (error) => { |
| 111 | + if (error) { |
| 112 | + throw error; |
| 113 | + } |
| 114 | + |
| 115 | + if (useHttps) { |
| 116 | + console.log( |
| 117 | + `\n🔒 HTTPS 开发服务器已启动!\n` + |
| 118 | + ` 本地访问: https://localhost:${port}\n` + |
| 119 | + ` 局域网访问: https://${getLocalIP()}:${port}\n` + |
| 120 | + ' (请在局域网设备上信任此自签名证书)\n' |
| 121 | + ); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + console.log( |
| 126 | + `\n🚀 HTTP 开发服务器已启动!\n` + |
| 127 | + ` 本地访问: http://localhost:${port}\n` + |
| 128 | + ` 局域网访问: http://${getLocalIP()}:${port}\n` |
| 129 | + ); |
| 130 | + }); |
| 131 | +} |
| 132 | + |
| 133 | +void main(); |
0 commit comments