修复远程访问:监听所有网络接口 (0.0.0.0)

- 修改 app.listen() 绑定到 0.0.0.0 而不是默认的 localhost
- 自动检测并显示本机 IP 地址
- 更新启动脚本使用正确的数据库端口 (5433)
- 现在可以通过局域网访问 http://192.168.0.250:3000
This commit is contained in:
work-1
2026-02-15 15:13:20 +08:00
parent 71f03f4125
commit d75230522e
3 changed files with 117 additions and 3 deletions
+19 -2
View File
@@ -3,6 +3,7 @@ const { Pool } = require('pg');
const cors = require('cors');
const bodyParser = require('body-parser');
const { v4: uuidv4 } = require('uuid');
const os = require('os');
const app = express();
const PORT = process.env.PORT || 3000;
@@ -368,9 +369,25 @@ app.get('/health', async (req, res) => {
}
});
// 获取本机 IP 地址
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return 'localhost';
}
// 启动服务器
app.listen(PORT, () => {
console.log(`🚀 多智能体任务服务已启动: http://localhost:${PORT}`);
app.listen(PORT, '0.0.0.0', () => {
const localIP = getLocalIP();
console.log(`🚀 多智能体任务服务已启动`);
console.log(` 本地访问: http://localhost:${PORT}`);
console.log(` 远程访问: http://${localIP}:${PORT}`);
});
// 优雅关闭