NodeJS: How to get the server's port?

后端 未结 19 1590
醉梦人生
醉梦人生 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: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.

提交回复
热议问题