NodeJS: How to get the server's port?

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

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

提交回复
热议问题