express app server . listen all interfaces instead of localhost only

前端 未结 3 857
时光取名叫无心
时光取名叫无心 2020-12-14 05:47

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) {
         


        
相关标签:
3条回答
  • 2020-12-14 06:03

    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

    0 讨论(0)
  • 2020-12-14 06:16

    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');
    
    0 讨论(0)
  • 2020-12-14 06:22

    If you use 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');
    
    0 讨论(0)
提交回复
热议问题