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


  • options <Object>

    • IncomingMessage <http.IncomingMessage> 指定要使用的 IncomingMessage 类。 用于扩展原始的 IncomingMessage默认值: IncomingMessage
    • ServerResponse <http.ServerResponse> 指定要使用的 ServerResponse 类。 用于扩展原始的 ServerResponse默认值: ServerResponse
    • requestTimeout: 设置从客户端接收整个请求的超时值(以毫秒为单位)。 有关详细信息,请参阅 server.requestTimeout默认值: 300000
    • headersTimeout: 设置从客户端接收完整 HTTP 标头的超时值,以毫秒为单位。 有关详细信息,请参阅 server.headersTimeout默认值: 60000
    • keepAliveTimeout: 在完成写入最后一个响应之后,在套接字将被销毁之前,服务器需要等待额外传入数据的不活动毫秒数。 有关详细信息,请参阅 server.keepAliveTimeout默认值: 5000
    • connectionsCheckingInterval: 以毫秒为单位设置间隔值,以检查不完整请求中的请求和标头超时。 默认值: 30000
    • insecureHTTPParser <boolean> 使用不安全的 HTTP 解析器,当为 true 时接受无效的 HTTP 标头。 应避免使用不安全的解析器。 有关详细信息,请参阅 --insecure-http-parser默认值: false
    • maxHeaderSize <number> 可选地覆盖此服务器接收到的请求的 --max-http-header-size 值,即请求头的最大长度(以字节为单位)。 默认值: 16384 (16 KiB)。
    • noDelay <boolean> 如果设置为 true,则它会在收到新的传入连接后立即禁用 Nagle 算法。 默认值: true
    • keepAlive <boolean> 如果设置为 true,则它会在收到新的传入连接后立即在套接字上启用保持活动功能,类似于在 [socket.setKeepAlive([enable][, initialDelay])][socket.setKeepAlive(enable, initialDelay)] 中所做的事情。 默认值: false
    • keepAliveInitialDelay <number> 如果设置为正数,则它会设置在空闲套接字上发送第一个保持活跃探测之前的初始延迟。 默认值: 0
    • 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>

    • IncomingMessage <http.IncomingMessage> Specifies the IncomingMessage class to be used. Useful for extending the original IncomingMessage. Default: IncomingMessage.
    • ServerResponse <http.ServerResponse> Specifies the ServerResponse class to be used. Useful for extending the original ServerResponse. Default: ServerResponse.
    • requestTimeout: Sets the timeout value in milliseconds for receiving the entire request from the client. See server.requestTimeout for more information. Default: 300000.
    • headersTimeout: Sets the timeout value in milliseconds for receiving the complete HTTP headers from the client. See server.headersTimeout for more information. Default: 60000.
    • 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.
    • connectionsCheckingInterval: Sets the interval value in milliseconds to check for request and headers timeout in incomplete requests. Default: 30000.
    • insecureHTTPParser <boolean> Use an insecure HTTP parser that accepts invalid HTTP headers when true. Using the insecure parser should be avoided. See --insecure-http-parser for more information. Default: false
    • 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> If set to true, it disables the use of Nagle's algorithm immediately after a new incoming connection is received. Default: true.
    • 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> 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> 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);