server.listen(options[, callback])


如果指定了 port 参数,该方法的行为跟 [server.listen([port][, hostname][, backlog][, callback])][`server.listen(port, host)`] 一样。否则,如果指定了 path 参数,该方法的行为与 server.listen(path[, backlog][, callback]) 一致。如果没有 port 或者 path 参数,则会抛出一个错误。

如果 exclusivefalse(默认),则集群的所有进程将使用相同的底层句柄,允许共享连接处理任务。如果 exclusivetrue,则句柄不会被共享,如果尝试端口共享将导致错误。监听独立端口的例子如下。

server.listen({
  host: 'localhost',
  port: 80,
  exclusive: true
});

If port is specified, it behaves the same as server.listen([port[, host[, backlog]]][, callback]). Otherwise, if path is specified, it behaves the same as server.listen(path[, backlog][, callback]). If none of them is specified, an error will be thrown.

If exclusive is false (default), then cluster workers will use the same underlying handle, allowing connection handling duties to be shared. When exclusive is true, the handle is not shared, and attempted port sharing results in an error. An example which listens on an exclusive port is shown below.

server.listen({
  host: 'localhost',
  port: 80,
  exclusive: true
});

Starting an IPC server as root may cause the server path to be inaccessible for unprivileged users. Using readableAll and writableAll will make the server accessible for all users.