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

18

115

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.

Nereus answered 30/3, 2011 at 17:38 Comment(0)
U
126

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.

Unshod answered 30/3, 2011 at 17:44 Comment(13)
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.Nereus
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.Photovoltaic
@Gisborne - did you try this - in fact, did you ever get home? We are all waiting here with baited breath!Unshod
@Photovoltaic 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.Aetna
is it possible to replace my network ip by some string like instead of 192.168.0.3:8000 it would be mycoolapp.xyz ?Veterinarian
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 ?Weimaraner
ng serve --host 0.0.0.0 did the magic for me to allow connection on localhostPhotoemission
I get this error: Error: listen EACCES: permission deniedPederson
@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.Snaggletooth
@ThomasMorris I solved the issue now. My parameters were reversed and it didn't like it - that why it showed up with that error.Pederson
Makes sense glad it was resolved. Be lovely if error codes could see this issueSnaggletooth
I don't understand how this works is your computer always making all ports available to your Wi-Fi? This seems unsecure can someone explain this process to me. I am a noob.Cavour
It's important to confirm you have the right firewall rules that allows ingress traffic on the specific port you are accessing from your LAN. TLDR: open the desired ports in your firewall.Reactant
A
64

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.

Allianora answered 31/3, 2018 at 16:30 Comment(5)
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 :)Euripus
@Euripus 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.Allianora
But this solution may not work in a Heroku app.Richthofen
this question wasn't even what I was looking for but you helped me figure out how people can play on my minecraft server lol (that I had just given into ppl using my computer hotspot for)Picador
make sure to install http package as well : dev.to/cmexdev/… You can access across all devices using same ip addressAdah
A
44

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.

Adalineadall answered 28/11, 2014 at 7:16 Comment(3)
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"Guile
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.Floridafloridia
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.exeDithionite
L
33

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

Lemberg answered 3/2, 2019 at 7:16 Comment(1)
I'm using Chrome, and interestingly, this solution only seems to work for me if I open up the inspector's Network tab and then refresh the page.Arellano
D
24

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
Drava answered 13/6, 2017 at 12:0 Comment(0)
B
8

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!

Barbaric answered 30/3, 2011 at 17:52 Comment(1)
Ngrok.com is now the way to go.Barbra
B
6
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.

Broke answered 5/11, 2018 at 8:17 Comment(0)
K
4

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.

Kb answered 4/10, 2020 at 14:42 Comment(1)
this only works with nodejs installed in the machine, I have this electron app with nodejs running, upon installation nodejs not exist.Columbous
U
3

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.
Ul answered 23/11, 2019 at 13:24 Comment(2)
This is awesome, how did you manage to discover it? can you share with us the explanation of why is this working? :)Counterbalance
@Counterbalance I tried several things only to discover this worked!Ul
C
2

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!

Croton answered 10/12, 2012 at 13:16 Comment(1)
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.Richthofen
S
2

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

Schroder answered 13/3, 2020 at 0:19 Comment(0)
T
1

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

$ npx ngrok http 8000
Thymol answered 22/4, 2021 at 18:20 Comment(2)
only works with Ngrok account and authtoken.Racklin
Ngrok is good but too expensive and to complicated to setup selfhosted versionQuasijudicial
C
1

In Ubuntu you can fix this by allowing a specific port or port range:

sudo ufw allow PORT-NUMBER/tcp

example:

sudo ufw allow 3000/tcp

or a range:

sudo ufw allow 3000:3001/tcp
Colloid answered 5/8, 2022 at 10:15 Comment(1)
please add an explanation of what this does and why you should do thisMicah
C
0

First, check your ipv4 address. In my case my ipv4 address is 192.168.44.112. If you don't know your ipv4 address, run this command on cmd.

ipconfig

Follow this code...

const express = require('express');

const app = express();
const port = process.env.port || 8000

app.get('/', (req, res) => {
    res.send("Hello Network!")
});

app.listen(port, '192.168.77.112', ()=>{
    console.log(`Listening port on ${port}`)
});
Counterfactual answered 24/7, 2022 at 13:25 Comment(0)
D
0

Try with localtunnel:

npm install localtunnel

On main module:

import * as localtunnel from 'localtunnel';

async function bootstrap() {

  const port = process.env.PORT || 6020;

  (async () => {
    const tunnel = await localtunnel({ port: port });
  
    console.log('Tunnel started at ', tunnel.url);
    
    tunnel.on('error', (err) => {
      console.log('error => ', err);
    });
    
    tunnel.on('close', () => {
      // tunnels are closed
      console.log('tunnel closed.')
    });
  })();

  const app = await NestFactory.create(MainModule);  

  await app.listen(port);
}
bootstrap();
Drench answered 9/4 at 11:22 Comment(0)
D
-1
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/');
Dimerous answered 25/8, 2015 at 9:38 Comment(0)
C
-2

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.

Chemisorb answered 28/2, 2016 at 17:51 Comment(0)
C
-2

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

Crispen answered 8/6, 2016 at 11:52 Comment(2)
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.Pettiford
Sorry, I have no experience with that I'm afraid.Crispen

© 2022 - 2024 — McMap. All rights reserved.