简体   繁体   English

Node.js Express-以公共方式运行项目

[英]Node.js Express - run project as public

In most time I run project on Linux as public and don't have troubles with it. 在大多数时间里,我都是在Linux上公开运行项目的,因此不会遇到麻烦。 But now I try run project on Windows also as public . 但是现在我尝试在Windows上也以公共方式运行项目。 I created rules for Windows Firewall. 我为Windows防火墙创建了规则。 And when I try open page 192.168.0.106:3000 I get error, that server not working. 当我尝试打开页面192.168.0.106:3000时出现错误,该服务器无法正常工作。 How configure public Node.js Express project as public? 如何将公共Node.js Express项目配置为公共?

Thanks. 谢谢。

this.app.listen(process.env.PORT || 3000, '192.168.0.106', function () {
    console.log(chalk.green('Server started with port 3000'));
    var os = require('os');
    var interfaces = os.networkInterfaces();
    var addresses = [];
    for (var k in interfaces) {
        for (var k2 in interfaces[k]) {
            var address = interfaces[k][k2];
            if (address.family === 'IPv4' && !address.internal) {
                addresses.push(address.address);
            }
        }
    }
    console.log(addresses);
});

The second argument is the listening route you want to listen on. 第二个参数是您要监听的监听路径。 The easiest solution is to set it to be 0.0.0.0 in order to listen on all routes. 最简单的解决方案是将其设置为0.0.0.0 ,以便在所有路由上进行侦听。

this.app.listen(process.env.PORT || 3000, '0.0.0.0', function () {
    //...
})

Here is the documentation on the http.listen api. 这是http.listen api上的文档。 But when you specify a specific IP address that means that you will only be listening on that one route. 但是,当您指定一个特定的IP地址时,这意味着您将仅在该路由上进行监听。 When you try to call back to yourself as 192.168.0.x then you're calling it from another route. 当您尝试以192.168.0.x身份回叫自己时,您正在从另一条路线中对其进行呼叫。 Similarly so with 127.0.0.1 . 127.0.0.1也是如此。 Use 0.0.0.0 to listen on all routes. 使用0.0.0.0侦听所有路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM