如何在本地(LAN)广播我的 Node JS 应用程序

问题描述 投票:0回答:1

我正在我的 Ubuntu 机器上运行一个 Node JS 应用程序并且它可以工作,我可以访问该机器上的 localhost:8080 并且该应用程序会显示。但是网络上的任何其他计算机都无法访问它。

代码:

const portNumber = 8080
let app = express()

app.use(express.static(__dirname + '/public'))

app.get('/', function(req, res) { res.sendFile(__dirname + '/public/stocks.html') })

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))

let httpServer = http.createServer(app)

httpServer.listen(portNumber, function(){ console.log('listening on port:: ' + portNumber) })

等等...

我最初使用端口 4023,但后来切换到 8080。我还查看了我的网络诊断以专门允许此应用程序,但在我的 AT&T 网络配置中找不到选项

AT&T firewall config page

我安装了 pm2 和 nginx,但这些不一定能帮助我解决这个问题,直到我可以解锁该端口/在我的 LAN 上解锁所有端口。

提前谢谢您!

javascript node.js hosting lan
1个回答
0
投票

您经常需要绑定到

"0.0.0.0"
才能使计算机外部的连接正常工作,如下所示:

httpServer.listen(portNumber, "0.0.0.0", ...)

默认绑定仅到localhost,这对其他机器没有用。

© www.soinside.com 2019 - 2024. All rights reserved.