NodeJS: How to get the server's port?

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

    With latest node.js (v0.3.8-pre): I checked the documentation, inspected the server instance returned by http.createServer(), and read the source code of server.listen()...

    Sadly, the port is only stored temporarily as a local variable and ends up as an argument in a call to process.binding('net').bind() which is a native method. I did not look further.

    It seems that there is no better way than keeping a reference to the port value that you provided to server.listen().

提交回复
热议问题