[英]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.