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

JavascriptNetworkingnode.js

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

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

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

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

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

Solution 6 - Javascript

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

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!

Solution 7 - Javascript

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.

Solution 8 - Javascript

And Don't Forget To Change in Index.html Following Code :

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

Solution 9 - Javascript

After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.

  1. Open "Windows Defender Firewall with Advanced Security"
  2. Select "Inbound Rules" in the left pane.
  3. In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".

Please see the attached screenshot:

enter image description here

After these changes, I am able to access my localhost using http://IPAddress:Port/ Thanks.

Solution 10 - Javascript

This worked for me and I think this is the most basic solution which involves the least setup possible:

  1. With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit ipconfig to get your ip address. Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:

> Wireless LAN adapter Wi-Fi: > > Connection-specific DNS Suffix . :
> Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
> IPv4 Address. . . . . . . . . . . : 192.168.1.2
> Subnet Mask . . . . . . . . . . . : 255.255.255.0

  1. Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
  2. Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.

Solution 11 - Javascript

put this codes in your server.js :

app.set('port', (80))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})

after that if you can't access app on network disable firewall like this :

enter image description here

Solution 12 - Javascript

ngrok allows you to expose a port on the internet with custom forwarding refs:

$ npx ngrok http 8000

Solution 13 - Javascript

I had this problem. The solution was to allow node.js through the server's firewall.

Solution 14 - Javascript

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/');

Solution 15 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontheabrahamView Question on Stackoverflow
Solution 1 - JavascriptBilly MoonView Answer on Stackoverflow
Solution 2 - JavascriptAlifView Answer on Stackoverflow
Solution 3 - JavascriptSungsoo KohView Answer on Stackoverflow
Solution 4 - JavascriptYatit ThakkerView Answer on Stackoverflow
Solution 5 - JavascriptGeo V LView Answer on Stackoverflow
Solution 6 - JavascriptalienhardView Answer on Stackoverflow
Solution 7 - JavascriptRamaView Answer on Stackoverflow
Solution 8 - JavascriptMohammad RiyazView Answer on Stackoverflow
Solution 9 - JavascriptJagadish DharanikotaView Answer on Stackoverflow
Solution 10 - JavascriptTNTView Answer on Stackoverflow
Solution 11 - JavascriptHossin AsaadiView Answer on Stackoverflow
Solution 12 - JavascriptWilkView Answer on Stackoverflow
Solution 13 - JavascriptAnthony CreganView Answer on Stackoverflow
Solution 14 - JavascriptSiddharthView Answer on Stackoverflow
Solution 15 - JavascriptSumant BagadeView Answer on Stackoverflow