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.