78

I'm very new for this stuff, and trying to make some express app

var express = require('express');
var app = express();

app.listen(3000, function(err) {
    if(err){
       console.log(err);
       } else {
       console.log("listen:3000");
    }
});

//something useful
app.get('*', function(req, res) {
  res.status(200).send('ok')
});

When I start the server with the command:

node server.js 

everything goes fine.

I see on the console

listen:3000

and when I try

curl http://localhost:3000

I see 'ok'.

When I try

telnet localhost

I see

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]' 

but when I try

netstat -na | grep :3000

I see

tcp  0  0 0.0.0.0:3000   0.0.0.0:*  LISTEN

The question is: why does it listen all interfaces instead of only localhost?

The OS is linux mint 17 without any whistles.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pavel L
  • 1,857
  • 3
  • 17
  • 23
  • 2
    [The fine manual](https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback) states: _"If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise"_. – robertklep Nov 27 '15 at 13:55
  • 1
    Thanks. it's another sign of "Explicit is better than implicit(c)", I think. – Pavel L Nov 27 '15 at 18:58
  • 2
    Given that accepting connections from the outside world is a basic premise for a server, listening to all interfaces (as opposed to loopback) seems to be a reasonable default to me :-) – robertklep Nov 27 '15 at 20:02

3 Answers3

144

If you don't specify host while calling app.listen, server will run on all interfaces available i.e on 0.0.0.0

You can bind the IP address using the following code

app.listen(3000, '127.0.0.1');

If you want to run server in all interface use the following code

app.listen(3000, '0.0.0.0');

or

app.listen(3000)
Vishnu
  • 11,614
  • 6
  • 51
  • 90
35

From the documentation: app.listen(port, [hostname], [backlog], [callback])

Binds and listens for connections on the specified host and port. This method is identical to Node’s http.Server.listen().

var express = require('express');
var app = express();
app.listen(3000, '0.0.0.0');
Julien Le Coupanec
  • 7,742
  • 9
  • 53
  • 67
  • what to give in a backlog param? – Muhammad Awais Sep 16 '21 at 18:18
  • 2
    If you are getting error that there is "no such overload" when using Typescript, then ensure the first param is really number and not string. Typically we pass the first param as `process.env.PORT` which is actually string. Change that to `parseInt(process.env.PORT || '8080')`. – AppleGrew Sep 01 '22 at 02:58
17

document: app.listen([port[, host[, backlog]]][, callback])

example:

const express = require('express');
const app = express();
app.listen('9000','0.0.0.0',()=>{
      console.log("server is listening on 9000 port");
})

Note: 0.0.0.0 to be given as host in order to access from outside interface

Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31