NodeJS: How to get the server's port?

后端 未结 19 1392
醉梦人生
醉梦人生 2020-12-02 05:47

You often see example hello world code for Node that creates an Http Server, starts listening on a port, then followed by something along the lines of:

conso         


        
相关标签:
19条回答
  • 2020-12-02 06:21

    In the current version (v0.5.0-pre) the port seems to be available as a property on the server object, see http://nodejs.org/docs/v0.4.7/api/net.html#server.address

    var server = http.createServer(function(req, res) {
        ...
    }
    
    server.listen(8088);
    console.log(server.address());
    console.log(server.address().address);
    console.log(server.address().port);
    

    outputs

    { address: '0.0.0.0', port: 8088 }
    0.0.0.0
    8088
    
    0 讨论(0)
  • 2020-12-02 06:21

    You can get the port number by using server.address().port like in below code:

    var http = require('http');
    var serverFunction = function (req, res) {
    
        if (req.url == '/') {
            console.log('get method');
            res.writeHead(200, { 'content-type': 'text/plain' });
            res.end('Hello World');
        }
    
    }
    var server = http.createServer(serverFunction);
    server.listen(3002, function () {
        console.log('server is listening on port:', server.address().port);
    });
    
    0 讨论(0)
  • 2020-12-02 06:23

    The simplest way to convert from the old style to the new (Express 3.x) style is like this:

    var server = app.listen(8080);
    console.log('Listening on port: ' + server.address().port);
    

    Pre 3.x it works like this:

    /* This no longer works */
    app.listen(8080);
    console.log('Listening on port: ' + app.address().port);
    
    0 讨论(0)
  • 2020-12-02 06:23

    If you did not define the port number and you want to know on which port it is running.

    let http = require('http');
    let _http = http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Hello..!')
    }).listen();
    console.log(_http.address().port);
    

    FYI, every time it will run in a different port.

    0 讨论(0)
  • 2020-12-02 06:25

    Requiring the http module was never necessary.

    An additional import of http is not necessary in Express 3 or 4. Assigning the result of listen() is enough.

    var server = require('express')();
    
    server.get('/', function(req, res) {
      res.send("Hello Foo!");
    });
    
    var listener = server.listen(3000);
    console.log('Your friendly Express server, listening on port %s', listener.address().port);
    // Your friendly Express server, listening on port 3000
    

    Again, this is tested in Express 3.5.1 & 4.0.0. Importing http was never necessary. The listen method returns an http server object. https://github.com/visionmedia/express/blob/master/lib/application.js#L531

    0 讨论(0)
  • 2020-12-02 06:28

    I use this way Express 4:

    app.listen(1337, function(){
      console.log('Express listening on port', this.address().port);
    });
    

    By using this I don't need to use a separate variable for the listener/server.

    0 讨论(0)
提交回复
热议问题