How could others, on a local network, access my NodeJS app while it's running on my machine?

213,211

Solution 1

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

http://your.network.ip.address:port/

e.g.

http://192.168.0.3:3000

Check you have the correct port - and the IP address on the network - not the internet 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.

Solution 2

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 yourport is forwarded to your ipv4 address.

  3. On Windows Firewall, tick all on Node.js:Server-side JavaScript.

Solution 3

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.

Solution 4

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 (127.0.0.1).

For example,

gatsby develop --host 0.0.0.0

yarn 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

Solution 5

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
Share:
213,211

Related videos on Youtube

theabraham
Author by

theabraham

Updated on March 31, 2022

Comments

  • theabraham
    theabraham about 2 years

    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.

  • theabraham
    theabraham about 13 years
    I switched to port to 8000, but I may have used my internet IP instead of the network's. I'll try this when I get home and report anything.
  • RobertPitt
    RobertPitt about 13 years
    You should also go into your router and allow your ip,192.168.0.3 to be able to forward port 8000, this way when your clients connect to your local ip, the router will allow them threw.
  • Billy Moon
    Billy Moon about 13 years
    @Gisborne - did you try this - in fact, did you ever get home? We are all waiting here with baited breath!
  • Ricardo Tomasi
    Ricardo Tomasi about 13 years
    @RobertPitt port forwarding is usually just from the outside world to the local network. Computers in the same local network have no restrictions in default (and most common) router configurations.
  • Rishabh Agrawal
    Rishabh Agrawal about 8 years
    is it possible to replace my network ip by some string like instead of 192.168.0.3:8000 it would be mycoolapp.xyz ?
  • tnishada
    tnishada about 8 years
    I also had the same issue. this is working for the local area network ip but not working with internet ip. so i can not connect through internet. Why it can not be used internet ip ?
  • Dema
    Dema over 7 years
    Ngrok.com is now the way to go.
  • DaddyMoe
    DaddyMoe over 7 years
    ng serve --host 0.0.0.0 did the magic for me to allow connection on localhost
  • DRAB
    DRAB over 7 years
    Interestingly, the application specified by "Evented I/O for V8 Javascript" is a standalone version of Node added by Adobe's Brackets editor. If you don't use Brackets, probably best not to allow this app. If you're running Node from a command line or another IDE (like Webstorm), the app you're looking for here is "Node.js: Server-Side JavaScript"
  • Lance
    Lance about 6 years
    How can you do that in a redhat server? My server only allow access to my node app when I turn off the firewalld service.
  • Anthony Cregan
    Anthony Cregan about 6 years
    Sorry, I have no experience with that I'm afraid.
  • Pramesh Bajracharya
    Pramesh Bajracharya almost 6 years
    I can't believe it was this simple. :) Thanks.
  • RyanOC
    RyanOC over 5 years
    This worked great. I can access my Parallels Windows VM from my Mac now, thanks!
  • danielsvane
    danielsvane about 5 years
    I had 3 different listings for Node in my setup, one for domain/private/public, but one of them was missing the checkbox on the left side. This solved my problem.
  • EdGzi
    EdGzi almost 4 years
    I get this error: Error: listen EACCES: permission denied
  • Thomas Morris
    Thomas Morris almost 4 years
    @LV98 Depending on how you are accessing it you will need to check firewalls or check you have permission to access the internet. Also you might need to go into the router to check its settings.
  • EdGzi
    EdGzi almost 4 years
    @ThomasMorris I solved the issue now. My parameters were reversed and it didn't like it - that why it showed up with that error.
  • Thomas Morris
    Thomas Morris almost 4 years
    Makes sense glad it was resolved. Be lovely if error codes could see this issue
  • AVAVT
    AVAVT over 3 years
    Just want to add that in my case Node.js: Server-Side JavaScript was not listed in the app list initially. I had to choose Allow another app... and explicitly chose nodejs.exe
  • Juliver Galleto
    Juliver Galleto over 2 years
    this only works with nodejs installed in the machine, I have this electron app with nodejs running, upon installation nodejs not exist.
  • kaushalpranav
    kaushalpranav over 2 years
    Only this answer worked for me. Can you add a comment on how localhost is different in this case from the ipv4 address like 192.168.0.3 . Thanks :)
  • Alif
    Alif over 2 years
    @kaushalpranav localhost points to the IP address 127.0.0.1 which is is the IP address for any machine. A program in that same machine can access to the server hosted at 127.0.0.1. To expose the server to other computers in that router network, you have to use the IP assigned by the router; that is 192.168.0.x or something like this. In this stage, the server can only be accessed from any computers in the router network. However, that server is not yet available to the internet.
  • Timo
    Timo about 2 years
    But this solution may not work in a Heroku app.
  • Timo
    Timo about 2 years
    I think your answer can be improved. Where is the io package coming from? Is it a jquery package? How does it work, what can I do with socket? Maybe you can show a use case.
  • manneym
    manneym about 2 years
    This did the trick.