How could others, on a local network, access my No

2019-01-16 10:10发布

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.

I naively tried using this url: my-ip-address:8000 and it won't work.

11条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-16 10:45

If you are using a router then:

  1. Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');

    in my machine it is

    server.listen(3000, '192.168.0.3');
    
  2. Make sure your port is forwarded to your ipv4 address.

  3. On Windows Firewall, tick all on Node.js:Server-side JavaScript.
查看更多
贪生不怕死
3楼-- · 2019-01-16 10:51
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.

查看更多
走好不送
4楼-- · 2019-01-16 10:53

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://my-ip-address:port

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

查看更多
萌系小妹纸
5楼-- · 2019-01-16 10:54

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.

  1. Go to windows button
  2. Search "Firewall"
  3. Choose "Allow programs to communicate through Firewall"
  4. Click Change Setup
  5. Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side 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.

查看更多
Emotional °昔
6楼-- · 2019-01-16 10:54

Faced similar issue with my Angular Node 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.

For this I updated the ng serve like this

//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

http://192.168.1.104:4201  
//http://{ipaddress}:4201
查看更多
三岁会撩人
7楼-- · 2019-01-16 10:54

Make sure the server is running, either by using 'npm start', or 'nodemon' in the command line window.

查看更多
登录 后发表回答