http.createServer([options][, requestListener])
-
options
<Object>-
IncomingMessage
<http.IncomingMessage> 指定要使用的IncomingMessage
类。用于扩展原始的IncomingMessage
。默认值:IncomingMessage
。¥
IncomingMessage
<http.IncomingMessage> Specifies theIncomingMessage
class to be used. Useful for extending the originalIncomingMessage
. Default:IncomingMessage
. -
ServerResponse
<http.ServerResponse> 指定要使用的ServerResponse
类。用于扩展原始的ServerResponse
。默认值:ServerResponse
。¥
ServerResponse
<http.ServerResponse> Specifies theServerResponse
class to be used. Useful for extending the originalServerResponse
. Default:ServerResponse
. -
insecureHTTPParser
<boolean> 使用不安全的 HTTP 解析器,当为true
时接受无效的 HTTP 标头。应避免使用不安全的解析器。有关详细信息,请参阅--insecure-http-parser
。默认值:false
¥
insecureHTTPParser
<boolean> Use an insecure HTTP parser that accepts invalid HTTP headers whentrue
. Using the insecure parser should be avoided. See--insecure-http-parser
for more information. Default:false
-
maxHeaderSize
<number> 可选地覆盖此服务器接收到的请求的--max-http-header-size
值,即请求标头的最大长度(以字节为单位)。默认值:16384 (16 KiB)。¥
maxHeaderSize
<number> Optionally overrides the value of--max-http-header-size
for requests received by this server, i.e. the maximum length of request headers in bytes. Default: 16384 (16 KiB). -
noDelay
<boolean> 如果设置为true
,则它会在收到新的传入连接后立即禁用 Nagle 算法。默认值:false
。¥
noDelay
<boolean> If set totrue
, it disables the use of Nagle's algorithm immediately after a new incoming connection is received. Default:false
. -
keepAlive
<boolean> 如果设置为true
,则在收到新的传入连接后立即启用套接字上的保持活动功能,与 [socket.setKeepAlive([enable][, initialDelay])
][socket.setKeepAlive(enable, initialDelay)
] 中的操作类似。默认值:false
。¥
keepAlive
<boolean> If set totrue
, it enables keep-alive functionality on the socket immediately after a new incoming connection is received, similarly on what is done in [socket.setKeepAlive([enable][, initialDelay])
][socket.setKeepAlive(enable, initialDelay)
]. Default:false
. -
keepAliveInitialDelay
<number> 如果设置为正数,则它会设置在空闲套接字上发送第一个保持活跃探测之前的初始延迟。默认值:0
。¥
keepAliveInitialDelay
<number> If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket. Default:0
. -
uniqueHeaders
<Array> 只应发送一次的响应标头列表。如果标头的值是数组,则子项将使用;
连接。¥
uniqueHeaders
<Array> A list of response headers that should be sent only once. If the header's value is an array, the items will be joined using;
.
-
-
requestListener
<Function> -
¥Returns: <http.Server>
返回 http.Server
的新实例。
¥Returns a new instance of http.Server
.
requestListener
是自动添加到 'request'
事件的函数。
¥The requestListener
is a function which is automatically
added to the 'request'
event.
const http = require('node:http');
// Create a local server to receive data from
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);
const http = require('node:http');
// Create a local server to receive data from
const server = http.createServer();
// Listen to the request event
server.on('request', (request, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);