http.createServer([options][, requestListener])


  • options <Object>

    • connectionsCheckingInterval:以毫秒为单位设置间隔值,以检查不完整请求中的请求和标头超时。默认值:30000

      ¥connectionsCheckingInterval: Sets the interval value in milliseconds to check for request and headers timeout in incomplete requests. Default: 30000.

    • headersTimeout:设置从客户端接收完整 HTTP 标头的超时值,以毫秒为单位。有关详细信息,请参阅 server.headersTimeout。默认值:60000

      ¥headersTimeout: Sets the timeout value in milliseconds for receiving the complete HTTP headers from the client. See server.headersTimeout for more information. Default: 60000.

    • highWaterMark <number> 可选择覆盖所有 socket' readableHighWaterMarkwritableHighWaterMark。这会影响 IncomingMessageServerResponsehighWaterMark 属性。默认值:参见 stream.getDefaultHighWaterMark()

      ¥highWaterMark <number> Optionally overrides all sockets' readableHighWaterMark and writableHighWaterMark. This affects highWaterMark property of both IncomingMessage and ServerResponse. Default: See stream.getDefaultHighWaterMark().

    • insecureHTTPParser <boolean> 如果设置为 true,它将使用启用了宽大标志的 HTTP 解析器。应避免使用不安全的解析器。有关详细信息,请参阅 --insecure-http-parser。默认值:false

      ¥insecureHTTPParser <boolean> If set to true, it will use a HTTP parser with leniency flags enabled. Using the insecure parser should be avoided. See --insecure-http-parser for more information. Default: false.

    • IncomingMessage <http.IncomingMessage> 指定要使用的 IncomingMessage 类。用于扩展原始的 IncomingMessage。默认值:IncomingMessage

      ¥IncomingMessage <http.IncomingMessage> Specifies the IncomingMessage class to be used. Useful for extending the original IncomingMessage. Default: IncomingMessage.

    • joinDuplicateHeaders <boolean> 如果设置为 true,则此选项允许使用逗号 (, ) 连接请求中多个标头的字段行值,而不是丢弃重复项。欲了解更多信息,请参阅 message.headers。默认值:false

      ¥joinDuplicateHeaders <boolean> If set to true, this option allows joining the field line values of multiple headers in a request with a comma (, ) instead of discarding the duplicates. For more information, refer to message.headers. Default: false.

    • keepAlive <boolean> 如果设置为 true,则在收到新的传入连接后立即启用套接字上的保持活动功能,与 [socket.setKeepAlive([enable][, initialDelay])][socket.setKeepAlive(enable, initialDelay)] 中的操作类似。默认值:false

      ¥keepAlive <boolean> If set to true, 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.

    • keepAliveTimeout:在完成写入最后一个响应之后,在套接字将被销毁之前,服务器需要等待额外传入数据的不活动毫秒数。有关详细信息,请参阅 server.keepAliveTimeout。默认值:5000

      ¥keepAliveTimeout: The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed. See server.keepAliveTimeout for more information. Default: 5000.

    • 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 算法。默认值:true

      ¥noDelay <boolean> If set to true, it disables the use of Nagle's algorithm immediately after a new incoming connection is received. Default: true.

    • requestTimeout:设置从客户端接收整个请求的超时值(以毫秒为单位)。有关详细信息,请参阅 server.requestTimeout。默认值:300000

      ¥requestTimeout: Sets the timeout value in milliseconds for receiving the entire request from the client. See server.requestTimeout for more information. Default: 300000.

    • requireHostHeader <boolean> 如果设置为 true,它会强制服务器以 400(错误请求)状态代码响应任何缺少主机标头(按照规范的规定)的 HTTP/1.1 请求消息。默认值:true

      ¥requireHostHeader <boolean> If set to true, it forces the server to respond with a 400 (Bad Request) status code to any HTTP/1.1 request message that lacks a Host header (as mandated by the specification). Default: true.

    • ServerResponse <http.ServerResponse> 指定要使用的 ServerResponse 类。用于扩展原始的 ServerResponse。默认值:ServerResponse

      ¥ServerResponse <http.ServerResponse> Specifies the ServerResponse class to be used. Useful for extending the original ServerResponse. Default: ServerResponse.

    • 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 ; .

    • rejectNonStandardBodyWrites <boolean> 如果设置为 true,则在写入没有正文的 HTTP 响应时会抛出错误。默认值:false

      ¥rejectNonStandardBodyWrites <boolean> If set to true, an error is thrown when writing to an HTTP response which does not have a body. Default: false.

  • requestListener <Function>

  • 返回:<http.Server>

    ¥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.

import http from '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((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    data: 'Hello World!',
  }));
});

server.listen(8000);
import http from '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);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);