简体   繁体   中英

How to get the link of the created server in the http.createServer() function?

How to get the link of the created server in the http.createServer() function?

I am trying to use the http package from node.js and use the createServer() function, then I want to print the server's link with the port, like this:

const createServer = http.createServer();
const server = createServer.listen(port);

And then get the link from the createServer variable like http://localhost or from a repl.it/replit.com link . this is my code in JS:

const port = 1234;
const createServer = http.createServer();
const server = createServer.listen(port);
const websocket = new web({ httpServer: server });

console.log(createServer); // This doesnt print the server link

You can get the information about the server by calling server.address() :

console.dir(server.address());

//{ address: '::', family: 'IPv6', port: 1234 }

Since you didn't specify a host when creating the server, the default one will be used which according to the documentation :

If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM