How to access NodeJS server on LAN?

112,354

Solution 1

Most likely your node application is binding to the loopback IP address 127.0.0.1 instead of the "all IPs" 0.0.0.0 since this is the default behavior of listen. Specify both port and IP in your call like server.listen(80, '0.0.0.0'); and try again.

Solution 2

Get you current local network IP and, run http server like this:

server.listen(80, 'current_local_ip');

Solution 3

Doing following worked for me on a windows PC. Try this : open

Control Panel\System and Security\Windows Defender Firewall\Allowed apps

Next look for node.js in the list and click change settings > Make sure private access is checked and then click ok.

Solution 4

First, you need to add C:\Program Files (x86)\node to the list of trusted applications in your firewall.

Then, in your node app, you can write:

listen(3333, '172.24.14.26', function() {

or:

listen(3333, '0.0.0.0', function() {

or:

listen(3333, function() {

or:

listen(80, '172.24.14.26', function() {

or:

listen(80, '0.0.0.0', function() {

or:

listen(80, function() {

Each one of these 6 combinations work in my case: node.js on Windows Server 2016, protected by a company proxy.

Solution 5

I found a good solution for this problem. Rather than doing all the configurations (Setting firewall, forwarding port etc) I used localtunnel which is an utility for exposing local node server over Internet. You can use it for development,testing,sharing purpose, just don't use it for production.

First you have to install localtunnel as follows:

$npm install -g localtunnel

After that configure your node app such that your node server should be running on localhost. For ex:

server.listen(3000, function () {
console.log('Listening to port:  ' + port);
});

Note down your_port which in my case was 3000, and start your node server.

Open another terminal and type following command for running localtunnel.

$lt --port 3000

After this , in terminal you will get an URL which you can use it for development/testing purpose. This URL will be available on Internet so you can share it with others too. As long as your localtunnel is running, others can access your local node server.

For more configuration options/help you can go through documentation: https://www.npmjs.com/package/localtunnel

Share:
112,354

Related videos on Youtube

Joel Murphy
Author by

Joel Murphy

Welcome to my profile. http://joel-murphy.co.uk/

Updated on September 18, 2022

Comments

  • Joel Murphy
    Joel Murphy almost 2 years

    I'm not the most knowledgeable guy ever with networking, but here goes...

    I've created an application with NodeJS and I'd like to test the application on my LAN with my family. The application listens on port 1337 for connections and I can access the application fine through my own PC by typing localhost:1337, 192.168.0.3:1337 or even http://joel-pc:1337/ into my browser's address bar.

    I will be also be running apache alongside NodeJS, and I can access this fine by typing 192.168.0.3 or http://joel-pc/ into a browser's address bar as long as it's connected to the same network.

    Now here's the weird part; If I stop the apache service, change my node application to listen on port 80 (http) insted of 1337, it will be accessible on my pc by typing localhost, 192.168.0.3 or even http://joel-pc into my browser's address bar. However, I still can't access NodeJS on any other PC on my network apart from my own.

    I've tried creating an outbound rule within Windows 7 to allow access to port 1337, but I still can't get access to my NodeJS server on any other PC than my own, even if it's listening on port 80. Is there something obvious I'm missing out on here?

    • cheesemacfly
      cheesemacfly about 11 years
      What if, as a test only, you turn off your firewall?
    • Joel Murphy
      Joel Murphy about 11 years
      If I turn off my firewall I can access node perfectly, thank you :) But surely there must be a safer way of doing things?
    • Joel Murphy
      Joel Murphy about 11 years
      I've put it back on now, don't worry :) Should I post on there too? Or is there a way of moving my question over? thank you
    • Joel Murphy
      Joel Murphy about 11 years
      Cheers. I flagged it too because I read in another thread that it needs to be flagged by 3 users before any action is taken by a moderator
    • Joel Murphy
      Joel Murphy about 11 years
      Still not moved over? Where are the mods lol? :(
    • cheesemacfly
      cheesemacfly about 11 years
      The flag I raised has been deemed helpful, so not sure how long it can take!
    • cheesemacfly
      cheesemacfly about 11 years
      When you ran the server for the first time, didn't the firewall ask you to allow the program to run through or not?
    • Aaron C
      Aaron C about 6 years
      If you are on windows, you may need to allow the connection through Windows Firewall
    • Arlen Beiler
      Arlen Beiler over 4 years
      This is a normal problem and usually involves allowing incoming connections to node.exe for the desired network types. On a laptop I wouldn't recommend enabling it for public networks, so you can have a little extra safety on public wifi, though you still need to set the network as a public network to make use of this.
    • Sandburg
      Sandburg over 4 years
      On which default port is npm install making its connexion?
  • Joel Murphy
    Joel Murphy about 11 years
    If I do this, I still can't access Node on any other device on my LAN. My code looks something like this:var io = require('socket.io'); var express = require("express"); var app = express(); var server = require('http').createServer(app) io = io.listen('80', '0.0.0.0');
  • Raul Rene
    Raul Rene almost 10 years
    This worked in my case. On some wireless networks localhost works as a string, but in others you have to type it as 127.0.0.1
  • psulek
    psulek almost 9 years
    this works for me.
  • Arlen Beiler
    Arlen Beiler over 4 years
    This is incorrect in light of the question as currently written, because it can in fact be accessed on the local computer using the LAN address, which means it is listening on that address as well.
  • Arlen Beiler
    Arlen Beiler over 4 years
    The NodeJS docs state that if no IP is specified it will listen on wildcard (in order: :: or 0.0.0.0)