简体   繁体   English

如何在http.createServer() 函数中获取创建的服务器的链接?

[英]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?如何在http.createServer() 函数中获取创建的服务器的链接?

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:我正在尝试使用 node.js 中的 http 包并使用 createServer() 函数,然后我想打印服务器与端口的链接,如下所示:

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 .然后从createServer变量(如http://localhost或 repl.it/replit.com 链接)获取链接。 this is my code in JS:这是我在 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() :您可以通过调用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 :由于您在创建服务器时没有指定host ,因此将使用默认host ,根据文档

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.如果省略主机,则当 IPv6 可用时,服务器将接受未指定 IPv6 地址 (::) 上的连接,否则接受未指定 IPv4 地址 (0.0.0.0) 上的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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