17

I am running a Node app that should be hosted on a local server. At the moment, I am sending just a plaintext response.

const http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200, {ContentType: 'text/plain'});
  res.end("test");
});

When I listen to the localhost everything works fine and I am able to send the request from my browser.

server.listen(3000, '127.0.0.1'); // works fine, on the same machine

However, if I try to listen to a port on my LAN network by typing the router's IP, I get an error.

server.listen(3000, '192.168.0.1'); // causes an error
Error: listen EADDRNOTAVAIL: address not available 192.168.0.1:3
000
    at Server.setupListenHandle [as _listen2] (net.js:1253:19)
    at listenInCluster (net.js:1318:12)
    at doListen (net.js:1451:7)
    at process._tickCallback (internal/process/next_tick.js:63:1
9)
    at Function.Module.runMain (internal/modules/cjs/loader.js:7
57:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1297:8)
    at process._tickCallback (internal/process/next_tick.js:63:1
9)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

I have tried this with my public IP address unsuccessfully. Is there any way to listen to a port on a LAN server so that I can send requests from any computer on the network?
Also, I would later like my application to run on any computer on any LAN network. How can I dynamically add my host?

Nikola
  • 400
  • 1
  • 3
  • 12
  • 1
    Have you try IP 0.0.0.0 (will listen on all IP) ? server.listen(3000, '0.0.0.0'); – Xavier Brassoud Apr 07 '20 at 18:27
  • You need set you server ip not your router ip, then you need to open a port in your router and add one port forwarding to your server from your router. For example if you server is listening at port 80 and you have port 5458 listening in your router you should redirect to your server ip plus its port like (192.168.0.x:80)... – David Agustin Melgar Apr 07 '20 at 18:33
  • 1
    @DavidAgustinMelgar How do I now which port is listening in my router? Where do I do the redirect? What exact code should be in my app? A bit clearer explanation would be helpful. Thanks – Nikola Apr 07 '20 at 18:44
  • you need to open it, go to Port Forward in the router's setting and there you need set the router port to machine IP eg router:8080 to => 192.168.0.4:80 8080 is an example. This is for accessing from internet to you webserver in you LAN. – David Agustin Melgar Apr 07 '20 at 19:17

5 Answers5

19

Try this

server.listen(3000, '0.0.0.0');

you can access your node application from other machine

e.g

your lan ip is 192.168.0.101 then you can browse 192.168.0.101:3000 from other machine

Shubham
  • 756
  • 4
  • 13
5

I got this error, the issue was I had a VPN connected

Rene Ricardo
  • 51
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '21 at 04:54
  • 1
    Turning off the VPN works for me, I just turn it on again once NUXT has started building. Sometimes it though happens even if I have no VPN connected and even if I restart the laptop. – Gabriel Jun 09 '22 at 07:01
4

Be careful with your IP. Sometimes it changes. When I work with Node I have to check the ipconfig in Shell... I hope with that it could help if someone else have the same problem...

  • Wow, it worked, thanks! I thought googling "what is my ip" would give me the correct ip but that wasn't the case. It didn't give me the IPv4 address. So, ipconfig resolved the issue for me – gignu Jul 25 '23 at 12:39
1

The right IP address actually turned out to be 192.168.0.104.
The IP address of a server machine should be used, not that of a router.

Nikola
  • 400
  • 1
  • 3
  • 12
0

Another reason

Sometimes this will happen when you are running the app inside a container and try to use your private ip (e.g 192.168.10.2) inside the container. Since container is an isolated environment, It doesn't know about your private ip.

Solution

You should use 0.0.0.0:PORT to run the node js app and then bind this port to your private ip. For example in docker compose you should do it like this:

ports:
   - "192.168.10.4:PORT:PORT"
Sajad Abdollahi
  • 1,593
  • 1
  • 7
  • 19