Node.js如何选择随机端口? [英] How does Node.js choose random ports?

查看:512
本文介绍了Node.js如何选择随机端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Node.js,我们可以创建服务器并在随机端口上进行侦听:

With Node.js, we can create a server and listen on a random port:

var server = net.createServer();
server.listen(0, '127.0.0.1');

第一个参数端口0表示选择一个随机端口,127.0.0.1表示仅在本地主机上监听,已记录.

The first parameter, port 0, indicates choose a random port, and 127.0.0.1 indicates to listen on localhost only, as documented.

Node.js是否选择未使用的端口?我是否必须检查一下自己并重试Node.js是否碰巧选择了一个已经打开并绑定到另一个应用程序的端口?它选择任何旧端口还是仅选择用户端口(> 1024)?

Does Node.js select a port that isn't in use? Do I have to check that myself and retry if Node.js happens to pick a port that is already open and bound to another application? Does it pick any old port, or only userland ports (>1024)?

推荐答案

Node.js是否选择未使用的端口?

Does Node.js select a port that isn't in use?

是,该端口将是可用端口. 操作系统选择一个未使用的端口,而不是Node.js,但是从最终用户的角度来看,这大致相同.

Yes, the port will be an available port. The operating system selects a port that isn't in use rather than Node.js, but from the end-user perspective, that's more-or-less the same thing.

毫无疑问,在最初于2012年发布此问题时,文档的措词有所不同,但截至目前(2019年1月),

The documentation no doubt had different wording at the time this question was originally posted in 2012, but as of now (January 2019), it is explicit about this: "If port is omitted or is 0, the operating system will assign an arbitrary unused port...".

我是否必须检查一下自己并重试Node.js是否碰巧选择了一个已经打开并绑定到另一个应用程序的端口?

Do I have to check that myself and retry if Node.js happens to pick a port that is already open and bound to another application?

不,您不会.无论如何,您都应该处理错误,因为许多事情都可能出错.但是,无需编写额外的代码来测试端口的可用性.

No, you do not. You should handle errors anyway as any number of things can go wrong. But writing extra code to test for port availability is not something you need to do.

它选择任何旧端口还是仅选择用户端口(> 1024)?

Does it pick any old port, or only userland ports (>1024)?

据我所知,它将永远是一个没有特权的端口.

As far as I know, it will always be an unprivileged port.

对于像macOS这样的操作系统,它顺序分配可用端口,下面的代码显示该操作系统将在端口不可用时跳过该端口.

For operating systems like macOS that assign available ports sequentially, here is code that shows that the operating system will skip a port if it is unavailable.

// Testing for macOS, which supplies available ports sequentially.

var net = require('net');

createServer(0, function () {
  var port = this.address().port;
  console.log('server was assigned port ' + port);
  createServer(port+1, function () {
    var port = this.address().port;
    console.log('server was assigned port ' + port);
    createServer(0, function () {
      var port = this.address().port;
      // This line will show that the OS skipped the occupied port and assigned the next available port.
      console.log('server was assigned port ' + port);
    });
  });
});

function createServer(port, callback) {
  console.log('create server with port ' + port);
  var server = net.createServer();
  server.listen(port, callback).unref();
}

这篇关于Node.js如何选择随机端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆