server.listen(options[, callback])
options
<Object> 必须。支持以下参数属性:port
<number>host
<string>path
<string> 如果指定了port
参数则会被忽略。查看识别 IPC 连接的路径。。backlog
<number>server.listen()
函数的通用参数。exclusive
<boolean> 默认值:false
。readableAll
<boolean> 对于 IPC 服务器,使管道对所有用户都可读。默认值:false
。writableAll
<boolean> 对于 IPC 服务器,使管道对所有用户都可写。默认值:false
。ipv6Only
<boolean> 对于 TCP 服务器,将ipv6Only
设置为true
将会禁用双栈支持,即绑定到主机::
不会使0.0.0.0
绑定。默认值:false
。
callback
<Function>- 返回: <net.Server>
如果指定了 port
参数,该方法的行为跟 server.listen([port[, host[, backlog]]][, callback]) 一样。
否则,如果指定了 path
参数,该方法的行为与 server.listen(path[, backlog][, callback])
一致。
如果没有 port
或者 path
参数,则会抛出一个错误。
如果 exclusive
是 false
(默认),则集群的所有进程将使用相同的底层句柄,允许共享连接处理任务。如果 exclusive
是 true
,则句柄不会被共享,如果尝试端口共享将导致错误。监听独立端口的例子如下。
server.listen({
host: 'localhost',
port: 80,
exclusive: true
});
以 root 身份启动 IPC 服务器可能导致无特权用户无法访问服务器路径。
使用 readableAll
和 writableAll
将使所有用户都可以访问服务器。
options
<Object> Required. Supports the following properties:port
<number>host
<string>path
<string> Will be ignored ifport
is specified. See Identifying paths for IPC connections.backlog
<number> Common parameter ofserver.listen()
functions.exclusive
<boolean> Default:false
readableAll
<boolean> For IPC servers makes the pipe readable for all users. Default:false
.writableAll
<boolean> For IPC servers makes the pipe writable for all users. Default:false
.ipv6Only
<boolean> For TCP servers, settingipv6Only
totrue
will disable dual-stack support, i.e., binding to host::
won't make0.0.0.0
be bound. Default:false
.
callback
<Function> functions.- Returns: <net.Server>
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.