0

I have a server (10.0.0.12) and my laptop (10.0.0.2) on a local network.

When I run curl http://10.0.0.2:3000 on the server, it works fine. When I run curl http://10.0.0.12:3000 on my laptop, it doesn't work saying site is unavailable.

I am able to ping and ssh into the server from my laptop.

Here is my code to finish the connection:

app.set('port', (3000));

app.listen(app.get('port'), function(){
    console.log("Node app running on localhost:" + app.get('port'));
}

I've tried passing in an ip address to the listen() function, but made no difference. I tried passing in 10.0.0.12 (the ip address of the server), 127.0.0.1, and 0.0.0.0 all with the same result.

How can I host my node app on a local network and have everyone who is on the local network be able to access it through the browser?

EDIT: I'm running on CentOS 7.

EDIT2: When I run netstat -lnt, it says this:

tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN

Rob Avery IV
  • 3,562
  • 10
  • 48
  • 72

4 Answers4

0

Have you tried to just omit the IP address? It should then be available on the IP address of the machine it is running on and the specified port.

Holger Adam
  • 765
  • 12
  • 16
0

As suggested by HA. remove the IP.

As you can see from the documentation: https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).

P.S. Which is the OS on the server?

B3rn475
  • 1,057
  • 5
  • 14
0

Maybe you can try :

app.listen(3000, '0.0.0.0', function(){
    console.log("Node app running on 0.0.0.0:3000");
}
Sachacr
  • 704
  • 1
  • 9
  • 17
0

A possible issue could be you aren't using the http module?

var http = require('http').Server(app);

http.listen(3000, function () {
    console.log('App running on port 3000');
});

A good practice would be set the port like

app.set('port', (3000));

var http = require('http').Server(app);

http.listen(app.get('port'), function () {
    console.log('App running on port ' + app.get('port'));
});
Aaron
  • 193
  • 7
  • I tried that and it didn't work. Node run fine. It just when I try to access the app from a different source on the same network, it can gain access. – Rob Avery IV Apr 10 '15 at 17:06