简体   繁体   English

无法使用我的机器的IP地址运行我的Node.Js应用程序,但可以使用localhost

[英]Cannot run my Node.Js app with my machine's IP address, but can with localhost

So I have a node js app set up to listen on port 5050 of my machine: 所以我有一个节点js应用程序设置为侦听我的机器的端口5050:

So when I go to http://localhost:5050/myapp my app loads fine. 所以当我去http:// localhost:5050 / myapp我的应用程序加载正常。 I am using an express framwork, therefore my listen frame work is as follows: 我正在使用快速框架,因此我的监听框架工作如下:

var server = app.listen(5050, '0.0.0.0', function () {
    console.log("App started on port 5050")
});

I also did a netstat(Am using windows 10 machine) and there are postivly no loopbacks to the local ip address. 我也做了一个netstat(我使用的是Windows 10机器),并且本地没有环回到本地IP地址。

However when I execute my machines ip eg. 但是当我执行我的机器ip例如。 http://192.168.0.231:5050/myapp I keep getting: This site can't be reached http://192.168.0.231:5050/myapp我一直得到:无法访问此网站

EDIT: I dont think I was being very clear 编辑:我不认为我很清楚

I have firewall turned off, I have tried without having an IP address specifiedin app.listen. 我关闭了防火墙,我试过没有在app.listen中指定IP地址。 I do not think it is an issue of the code, as when I try with just some simple test app and try run with my IP address, this also times out 我不认为这是代码的问题,因为当我尝试使用一些简单的测试应用程序并尝试使用我的IP地址运行时,这也会超时

Try use listen without ip parameter 尝试使用listen without ip参数

var server = app.listen(5050, function () {
   console.log("App started on port 5050")
});  

While running a nodejs application, i also faced the same issue as you mentioned. 在运行nodejs应用程序时,我也遇到了与您提到的相同的问题。 Before my code looks like this. 在我的代码看起来像这样。

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoChat = require('./app/mongo_chat');

var ip = "127.0.0.1";
var port = 3000;
var users = {};
var WhisperChek = false;
var storage=[];
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
    res.sendfile('index.html');
});
//socket connection
    io.on('connection', function (socket) {
        //console.log('Someone connected!');
        mongoChat.mognolizer(io,socket, users, WhisperChek);
        function updateNicknames() {
            io.emit('nickname', Object.keys(users));
        }
//disconnect socket
        socket.on('disconnect', function (data) {
            if (!socket.Username)return;
            delete users[socket.Username];
            updateNicknames();
        });
    });
http.listen(port,ip, function () {
    console.log('listening on *:3000');
});

But, I removed the var ip="127.0.0.1" from the code and updated the listen function as: 但是,我从代码中删除了var ip="127.0.0.1"并将listen函数更新为:

http.listen(port, function () {
        console.log('listening on *:3000');
    });

Now, my application is running both on localhost ie, on localhost:3000 and its also running on my ipaddress ie, on ipaddress:3000 . 现在,我的应用程序在localhost上运行,即在localhost:3000运行,它也运行在我的ipaddress上,即在ipaddress:3000 The problem there is when we give ip as 127.0.0.1 it only runs on the localhost, as ipaddress we use is private and it changes with the change in network connection. 问题在于我们将ip命名为127.0.0.1它只在localhost上运行,因为我们使用的ipaddress是私有的,它随着网络连接的变化而变化。 Check it once whether it works for you or not. 检查一下它是否适合您。

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

相关问题 Node.js似乎忽略了我提供的IP地址,而是使用localhost打开服务器 - Node.js seems to ignore my IP address i give, instead it uses localhost to open the server 为什么我不能在运行由Openshift托管的Node.js + Websocket的服务器上获取连接客户端的IP地址(使用WebSocket-Node) - Why can't I get the connecting client's ip address on my server running Node.js + Websocket hosted by Openshift (Using WebSocket-Node) 为什么 Node.js 无法在终端中运行我的 JavaScript 代码? - Why Node.js cannot run my JavaScript code in the terminal? 保护我的Node.js应用程序的REST API? - Securing my Node.js app's REST API? 无法在Node中接收我的机器的本地IP地址 - Unable to receive local IP address of my machine in Node node.js http:获取远程服务器的IP地址 - node.js http(s): get ip address of remote server Node.JS:为什么我与 localhost:3000 的连接被拒绝? - Node.JS: Why is my connection to localhost:3000 refused? 将多个 Node.js 应用程序限制为具有多个 IPv4 地址的机器上的特定 IP 地址 - Restricting Multiple Node.js Apps to A Specific IP Address On Machine with Multiple IPv4 Addresses 在 Node.js 中获取本地 IP 地址 - Get local IP address in Node.js 如何更改node.js应用程序的IP和端口? - How to change node.js app's ip and port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM