http.createServer([options][, requestListener])
-
options
<Object>connectionsCheckingInterval
: 以毫秒为单位设置间隔值,以检查不完整请求中的请求和标头超时。 默认值:30000
。headersTimeout
: 设置从客户端接收完整 HTTP 标头的超时值,以毫秒为单位。 有关详细信息,请参阅server.headersTimeout
。 默认值:60000
。insecureHTTPParser
<boolean> 使用不安全的 HTTP 解析器,当为true
时接受无效的 HTTP 标头。 应避免使用不安全的解析器。 有关详细信息,请参阅--insecure-http-parser
。 默认值:false
。IncomingMessage
<http.IncomingMessage> 指定要使用的IncomingMessage
类。 用于扩展原始的IncomingMessage
。 默认值:IncomingMessage
。keepAlive
<boolean> 如果设置为true
,则它会在收到新的传入连接后立即在套接字上启用保持活动功能,类似于在 [socket.setKeepAlive([enable][, initialDelay])
][socket.setKeepAlive(enable, initialDelay)
] 中所做的事情。 默认值:false
。keepAliveInitialDelay
<number> 如果设置为正数,则它会设置在空闲套接字上发送第一个保持活跃探测之前的初始延迟。 默认值:0
。requestTimeout
: 设置从客户端接收整个请求的超时值(以毫秒为单位)。 有关详细信息,请参阅server.requestTimeout
。joinDuplicateHeaders
<boolean> 它使用,
连接请求中多个标头的字段行值,而不是丢弃重复项。 有关详细信息,请参阅message.headers
。 默认值:false
。ServerResponse
<http.ServerResponse> 指定要使用的ServerResponse
类。 用于扩展原始的ServerResponse
。 默认值:ServerResponse
。 默认值:300000
。uniqueHeaders
<Array> 只应发送一次的响应标头列表。 如果标头的值是数组,则子项将使用;
连接。
-
requestListener
<Function> -
返回: <http.Server>
返回 http.Server
的新实例。
requestListener
是自动添加到 'request'
事件的函数。
const http = require('node:http');
// 创建本地服务器来从其接收数据
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');
// 创建本地服务器来从其接收数据
const server = http.createServer();
// 监听请求事件
server.on('request', (request, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!',
}));
});
server.listen(8000);
-
options
<Object>connectionsCheckingInterval
: Sets the interval value in milliseconds to check for request and headers timeout in incomplete requests. Default:30000
.headersTimeout
: Sets the timeout value in milliseconds for receiving the complete HTTP headers from the client. Seeserver.headersTimeout
for more information. Default:60000
.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
.IncomingMessage
<http.IncomingMessage> Specifies theIncomingMessage
class to be used. Useful for extending the originalIncomingMessage
. Default:IncomingMessage
.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> If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket. Default:0
.requestTimeout
: Sets the timeout value in milliseconds for receiving the entire request from the client. Seeserver.requestTimeout
for more information.joinDuplicateHeaders
<boolean> It joins the field line values of multiple headers in a request with,
instead of discarding the duplicates. Seemessage.headers
for more information. Default:false
.ServerResponse
<http.ServerResponse> Specifies theServerResponse
class to be used. Useful for extending the originalServerResponse
. Default:ServerResponse
. Default:300000
.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>
Returns a new instance of http.Server
.
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);