How can i make a node.js server to listen to my ip on my lan instead of localhost

喜欢而已 提交于 2019-12-10 22:16:30

问题


I have a website built on top of expressjs and node.js which is running fine on my localhost. How can i make it to run on my ip(192.168.69.100) instead of localhost so that other users on my LAN can access it.?

Thanks in advance. :)

A snippet of my code

app.listen(port,function (err) {
if(err)
{
    console.log(err);
}

console.log('Running on ' + port);
});

回答1:


By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:

http://192.168.69.100:port

If that didn't work, you might have your IP address wrong.




回答2:


Might be good to give the device running node a fixed-ip if you're using DHCP; at some point your IP-address might change and the server is not accessible anymore. Alternatively you can use your hostname after some additional work.



来源:https://stackoverflow.com/questions/35669772/how-can-i-make-a-node-js-server-to-listen-to-my-ip-on-my-lan-instead-of-localhos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!