Javascript 本地网络上的其他人如何在我的机器上运行时访问我的 NodeJS 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5489956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could others, on a local network, access my NodeJS app while it's running on my machine?
提问by theabraham
I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
我有一个非常直接的问题。我用NodeJS做了一个网页游戏,可以自己玩多个浏览器窗口并排打开成功;但是,我想知道其他本地计算机是否也可以访问并与我一起玩游戏。
I naively tried using this url: my-ip-address:8000
and it won't work.
我天真地尝试使用这个 url:my-ip-address:8000
并且它不起作用。
回答by Billy Moon
your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
您的 node.js 服务器通常在脚本末尾确定的端口上运行。有时 3000。但可以是任何东西。其他人访问的正确方式如你所说......
http://your.network.ip.address:port/e.g. http://192.168.0.3:3000
http://your.network.ip.address:port/例如 http://192.168.0.3:3000
check you have the correct port - and the ip address on the network - not internet ip.
检查您的端口是否正确 - 以及网络上的 IP 地址 - 不是互联网 IP。
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
否则,您的路由器可能会阻止这些端口。尝试使用 8080 或 80 来解决这个问题 - 否则重新配置您的路由器。
回答by Sungsoo Koh
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
我有同样的问题并解决了这个问题。就我而言,Windows 防火墙(不是路由器)阻止了主机上的 V8 机器 I/O。
- Go to windows button
- Search "Firewall"
- Choose "Allow programs to communicate through Firewall"
- Click Change Setup
- Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"
- 转到窗口按钮
- 搜索“防火墙”
- 选择“允许程序通过防火墙通信”
- 单击更改设置
- 勾选所有“V8 Javascript 事件 I/O”或“Node.js:服务器端 Javascript”
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
我的猜测是“V8 Javascript 的事件 I/O”是 node.js 与外部世界通信的 I/O 进程,我们需要先释放它,然后它才能将数据包发送到本地计算机之外。启用此程序通过 Windows 防火墙进行通信后,我可以使用任何端口号进行侦听。
回答by Alif
If you are using a router then:
如果您使用的是路由器,则:
Replace
server.listen(yourport, 'localhost');
withserver.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure your port is forwarded to your ipv4 address.
- On Windows Firewall, tick all on Node.js:Server-side JavaScript.
替换
server.listen(yourport, 'localhost');
为server.listen(yourport, 'your ipv4 address');
在我的机器上是
server.listen(3000, '192.168.0.3');
确保您的端口转发到您的 ipv4 地址。
- 在 Windows 防火墙上,勾选所有 Node.js:Server-side JavaScript。
回答by Geo V L
Faced similar issue with my AngularNode Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201
worked fine in localhost. But http://{ipaddress}:4201
not working in other machines in local network.
我的AngularNode Server(v6.10.3) 在 WINdows 10 中设置时遇到了类似的问题。
http://localhost:4201
在 localhost 中运行良好。但 http://{ipaddress}:4201
不能在本地网络中的其他机器上工作。
For this I updated the ng serve
like this
为此,我更新了ng serve
这样的
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
完成此修改后,可以访问我在网络中其他机器上的应用程序,并调用此 url
http://192.168.1.104:4201
//http://{ipaddress}:4201
回答by Yatit Thakker
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (172.0.0.1).
一个尚未有人提及的技巧是记住将应用程序托管在 LAN 可访问地址 0.0.0.0 上,而不是默认的本地主机上。与默认本地主机地址 (172.0.0.1) 相比,Mac 和 Linux 上的防火墙对此地址的严格程度较低。
For example,
例如,
gatsby develop --host 0.0.0.0
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig
or ipconfig
in the terminal. Then try one of the IP addresses on the left that does not end in .255
or .0
然后,您可以通过输入ifconfig
或ipconfig
在终端中访问要连接的地址。然后尝试左侧不以.255
或结尾的 IP 地址之一.0
回答by alienhard
The port is probably blocked by your local firewall or router. Hard to tell without details.
该端口可能被您的本地防火墙或路由器阻止。没有细节很难说。
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
但是有一个简单的解决方案,您不必弄乱防火墙规则,将节点作为特权进程运行以在端口 80 上提供服务,等等...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
查看本地隧道。它是一个很棒的 Ruby 脚本/服务,它允许您在几秒钟内在 Internet 上提供任何本地端口。这对于生产设置当然没有用,但与同事一起尝试游戏,它应该可以正常工作!
回答by Rama
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig
in cmd if your Windows user else you can use ifconfig
.
ipconfig
如果您的 Windows 用户可以使用,您可以通过输入cmd来获取您的 IP 地址ifconfig
。
回答by Mohammad Riyaz
And Don't Forget To Change in Index.html Following Code :
并且不要忘记在 Index.html 中更改以下代码:
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
祝你好运!
回答by Siddharth
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
回答by Sumant Bagade
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:
默认情况下,节点将在运行它的主机公开的每个 IP 地址上运行。你不需要做任何特别的事情。您已经知道服务器在特定端口上运行。您可以通过在该机器上的浏览器上使用该 IP 地址来证明这一点:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
如果这不起作用,则您的 IP 地址可能有误。