const { execSync } = require('child_process'); const ip = require('./utils/ip'); const { port, command } = require('./config'); const express = require('express') const app = express() const { join } = require('path'); // 配置静态文件目录 // 这会将项目中的public文件夹设置为静态资源目录 app.use(express.static(join(__dirname, 'public'))); app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET, POST'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); next(); }); app.get('/ip', async (req, res) => { let output = execSync(command, { encoding: "utf8" }); res.json(await ip.parseNetworkInfo(output)) }) app.get('/', async (req, res) => { res.sendFile(join(__dirname, 'public', 'index.html')); }) app.listen(port, () => { console.log(`app listening on port: http://127.0.0.1:${port}`) })