2012-02-27 11:09:33 -08:00
|
|
|
|
# HTTP
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2017-01-22 19:16:21 -08:00
|
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
|
> Stability: 2 - Stable
|
2012-03-02 15:14:03 -08:00
|
|
|
|
|
2020-06-22 13:56:08 -04:00
|
|
|
|
<!-- source_link=lib/http.js -->
|
|
|
|
|
|
2024-05-29 20:46:42 -04:00
|
|
|
|
This module, containing both a client and server, can be imported via
|
|
|
|
|
`require('node:http')` (CommonJS) or `import * as http from 'node:http'` (ES module).
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-08-13 12:14:34 -04:00
|
|
|
|
The HTTP interfaces in Node.js are designed to support many features
|
2010-10-28 23:18:16 +11:00
|
|
|
|
of the protocol which have been traditionally difficult to use.
|
|
|
|
|
In particular, large, possibly chunk-encoded, messages. The interface is
|
2020-03-03 21:23:59 -08:00
|
|
|
|
careful to never buffer entire requests or responses, so the
|
2010-10-28 23:18:16 +11:00
|
|
|
|
user is able to stream data.
|
|
|
|
|
|
|
|
|
|
HTTP message headers are represented by an object like this:
|
|
|
|
|
|
2023-11-22 20:03:33 +01:00
|
|
|
|
```json
|
|
|
|
|
{ "content-length": "123",
|
|
|
|
|
"content-type": "text/plain",
|
|
|
|
|
"connection": "keep-alive",
|
|
|
|
|
"host": "example.com",
|
|
|
|
|
"accept": "*/*" }
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
|
|
|
|
Keys are lowercased. Values are not modified.
|
|
|
|
|
|
2020-02-11 15:03:31 -10:00
|
|
|
|
In order to support the full spectrum of possible HTTP applications, the Node.js
|
2010-10-28 23:18:16 +11:00
|
|
|
|
HTTP API is very low-level. It deals with stream handling and message
|
|
|
|
|
parsing only. It parses a message into headers and body but it does not
|
|
|
|
|
parse the actual headers or the body.
|
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
See [`message.headers`][] for details on how duplicate headers are handled.
|
2013-08-15 14:12:12 -07:00
|
|
|
|
|
|
|
|
|
The raw headers as they were received are retained in the `rawHeaders`
|
2018-04-02 08:38:48 +03:00
|
|
|
|
property, which is an array of `[key, value, key2, value2, ...]`. For
|
2013-08-15 14:12:12 -07:00
|
|
|
|
example, the previous message header object might have a `rawHeaders`
|
|
|
|
|
list like the following:
|
|
|
|
|
|
2024-04-15 17:08:10 +02:00
|
|
|
|
<!-- eslint-disable @stylistic/js/semi -->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-07-09 08:13:09 +03:00
|
|
|
|
```js
|
2016-01-17 18:39:07 +01:00
|
|
|
|
[ 'ConTent-Length', '123456',
|
|
|
|
|
'content-LENGTH', '123',
|
|
|
|
|
'content-type', 'text/plain',
|
|
|
|
|
'CONNECTION', 'keep-alive',
|
2022-02-03 01:39:01 -05:00
|
|
|
|
'Host', 'example.com',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
'accepT', '*/*' ]
|
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## Class: `http.Agent`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.4
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2017-01-09 12:45:46 -05:00
|
|
|
|
An `Agent` is responsible for managing connection persistence
|
|
|
|
|
and reuse for HTTP clients. It maintains a queue of pending requests
|
|
|
|
|
for a given host and port, reusing a single socket connection for each
|
|
|
|
|
until the queue is empty, at which time the socket is either destroyed
|
|
|
|
|
or put into a pool where it is kept to be used again for requests to the
|
|
|
|
|
same host and port. Whether it is destroyed or pooled depends on the
|
2021-07-04 20:39:17 -07:00
|
|
|
|
`keepAlive` [option](#new-agentoptions).
|
2017-01-09 12:45:46 -05:00
|
|
|
|
|
|
|
|
|
Pooled connections have TCP Keep-Alive enabled for them, but servers may
|
|
|
|
|
still close idle connections, in which case they will be removed from the
|
|
|
|
|
pool and a new connection will be made when a new HTTP request is made for
|
|
|
|
|
that host and port. Servers may also refuse to allow multiple requests
|
|
|
|
|
over the same connection, in which case the connection will have to be
|
|
|
|
|
remade for every request and cannot be pooled. The `Agent` will still make
|
|
|
|
|
the requests to that server, but each one will occur over a new connection.
|
|
|
|
|
|
|
|
|
|
When a connection is closed by the client or the server, it is removed
|
|
|
|
|
from the pool. Any unused sockets in the pool will be unrefed so as not
|
|
|
|
|
to keep the Node.js process running when there are no outstanding requests.
|
2019-10-02 00:31:57 -04:00
|
|
|
|
(see [`socket.unref()`][]).
|
2017-01-09 12:45:46 -05:00
|
|
|
|
|
|
|
|
|
It is good practice, to [`destroy()`][] an `Agent` instance when it is no
|
|
|
|
|
longer in use, because unused sockets consume OS resources.
|
|
|
|
|
|
2017-08-04 17:56:03 +08:00
|
|
|
|
Sockets are removed from an agent when the socket emits either
|
2017-03-03 15:45:20 -08:00
|
|
|
|
a `'close'` event or an `'agentRemove'` event. When intending to keep one
|
2017-08-04 17:56:03 +08:00
|
|
|
|
HTTP request open for a long time without keeping it in the agent, something
|
2017-03-03 15:45:20 -08:00
|
|
|
|
like the following may be done:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
http.get(options, (res) => {
|
|
|
|
|
// Do stuff
|
|
|
|
|
}).on('socket', (socket) => {
|
|
|
|
|
socket.emit('agentRemove');
|
|
|
|
|
});
|
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
An agent may also be used for an individual request. By providing
|
2017-01-09 12:45:46 -05:00
|
|
|
|
`{agent: false}` as an option to the `http.get()` or `http.request()`
|
|
|
|
|
functions, a one-time use `Agent` with default options will be used
|
|
|
|
|
for the client connection.
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
`agent:false`:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
http.get({
|
|
|
|
|
hostname: 'localhost',
|
|
|
|
|
port: 80,
|
|
|
|
|
path: '/',
|
2022-11-17 08:19:12 -05:00
|
|
|
|
agent: false, // Create a new agent just for this one request
|
2016-01-17 18:39:07 +01:00
|
|
|
|
}, (res) => {
|
|
|
|
|
// Do stuff with response
|
2016-07-14 22:41:29 -07:00
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `new Agent([options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.4
|
2020-05-07 08:18:11 +02:00
|
|
|
|
changes:
|
2021-05-11, Version 14.17.0 'Fermium' (LTS)
Notable Changes:
Diagnostics channel (experimental module):
`diagnostics_channel` is a new experimental module that provides an API
to create named channels to report arbitrary message data for
diagnostics purposes.
The module was initially introduced in Node.js v15.1.0 and is
backported to v14.17.0 to enable testing it at a larger scale.
With `diagnostics_channel`, Node.js core and module authors can publish
contextual data about what they are doing at a given time. This could
be the hostname and query string of a mysql query, for example. Just
create a named channel with `dc.channel(name)` and call
`channel.publish(data)` to send the data to any listeners to that
channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
MySQL.prototype.query = function query(queryString, values, callback) {
// Broadcast query information whenever a query is made
channel.publish({
query: queryString,
host: this.hostname,
});
this.doQuery(queryString, values, callback);
};
```
Channels are like one big global event emitter but are split into
separate objects to ensure they get the best performance. If nothing is
listening to the channel, the publishing overhead should be as close to
zero as possible. Consuming channel data is as easy as using
`channel.subscribe(listener)` to run a function whenever a message is
published to that channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
channel.subscribe(({ query, host }) => {
console.log(`mysql query to ${host}: ${query}`);
});
```
The data captured can be used to provide context for what an app is
doing at a given time. This can be used for things like augmenting
tracing data, tracking network and filesystem activity, logging
queries, and many other things. It's also a very useful data source
for diagnostics tools to provide a clearer picture of exactly what the
application is doing at a given point in the data they are presenting.
Contributed by Stephen Belanger (https://github.com/nodejs/node/pull/34895).
UUID support in the crypto module:
The new `crypto.randomUUID()` method now allows to generate random
[RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4
UUID strings:
```js
const { randomUUID } = require('crypto');
console.log(randomUUID());
// 'aa7c91a1-f8fc-4339-b9db-f93fc7233429'
```
Contributed by James M Snell (https://github.com/nodejs/node/pull/36729).
Experimental support for `AbortController` and `AbortSignal`:
Node.js 14.17.0 adds experimental partial support for `AbortController`
and `AbortSignal`.
Both constructors can be enabled globally using the
`--experimental-abortcontroller` flag.
Additionally, several Node.js APIs have been updated to support
`AbortSignal` for cancellation.
It is not mandatory to use the built-in constructors with them. Any
spec-compliant third-party alternatives should be compatible.
`AbortSignal` support was added to the following methods:
* `child_process.exec`
* `child_process.execFile`
* `child_process.fork`
* `child_process.spawn`
* `dgram.createSocket`
* `events.on`
* `events.once`
* `fs.readFile`
* `fs.watch`
* `fs.writeFile`
* `http.request`
* `https.request`
* `http2Session.request`
* The promisified variants of `setImmediate` and `setTimeout`
Other notable changes:
* doc:
* revoke deprecation of legacy url, change status to legacy (James M Snell) (https://github.com/nodejs/node/pull/37784)
* add legacy status to stability index (James M Snell) (https://github.com/nodejs/node/pull/37784)
* upgrade stability status of report API (Gireesh Punathil) (https://github.com/nodejs/node/pull/35654)
* deps:
* V8: Backport various patches for Apple Silicon support (BoHong Li) (https://github.com/nodejs/node/pull/38051)
* update ICU to 68.1 (Michaël Zasso) (https://github.com/nodejs/node/pull/36187)
* upgrade to libuv 1.41.0 (Colin Ihrig) (https://github.com/nodejs/node/pull/37360)
* http:
* add http.ClientRequest.getRawHeaderNames() (simov) (https://github.com/nodejs/node/pull/37660)
* report request start and end with diagnostics\_channel (Stephen Belanger) (https://github.com/nodejs/node/pull/34895)
* util:
* add getSystemErrorMap() impl (eladkeyshawn) (https://github.com/nodejs/node/pull/38101)
PR-URL: https://github.com/nodejs/node/pull/38507
2021-05-02 23:12:18 -04:00
|
|
|
|
- version:
|
|
|
|
|
- v15.6.0
|
|
|
|
|
- v14.17.0
|
2020-12-30 13:02:27 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/36685
|
|
|
|
|
description: Change the default scheduling from 'fifo' to 'lifo'.
|
2020-09-28 10:54:13 -07:00
|
|
|
|
- version:
|
|
|
|
|
- v14.5.0
|
|
|
|
|
- v12.19.0
|
2020-06-22 21:15:34 +08:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33617
|
|
|
|
|
description: Add `maxTotalSockets` option to agent constructor.
|
2020-11-16 12:04:38 -05:00
|
|
|
|
- version:
|
|
|
|
|
- v14.5.0
|
|
|
|
|
- v12.20.0
|
2020-05-07 08:18:11 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33278
|
|
|
|
|
description: Add `scheduling` option to specify the free socket
|
|
|
|
|
scheduling strategy.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
* `options` {Object} Set of configurable options to set on the agent.
|
|
|
|
|
Can have the following fields:
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `keepAlive` {boolean} Keep sockets around even when there are no
|
2017-01-09 12:45:46 -05:00
|
|
|
|
outstanding requests, so they can be used for future requests without
|
2019-03-03 12:28:17 +01:00
|
|
|
|
having to reestablish a TCP connection. Not to be confused with the
|
|
|
|
|
`keep-alive` value of the `Connection` header. The `Connection: keep-alive`
|
|
|
|
|
header is always sent when using an agent except when the `Connection`
|
|
|
|
|
header is explicitly specified or when the `keepAlive` and `maxSockets`
|
|
|
|
|
options are respectively set to `false` and `Infinity`, in which case
|
|
|
|
|
`Connection: close` will be used. **Default:** `false`.
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* `keepAliveMsecs` {number} When using the `keepAlive` option, specifies
|
2021-07-04 20:39:17 -07:00
|
|
|
|
the [initial delay][]
|
2017-01-09 12:45:46 -05:00
|
|
|
|
for TCP Keep-Alive packets. Ignored when the
|
2018-04-02 04:44:32 +03:00
|
|
|
|
`keepAlive` option is `false` or `undefined`. **Default:** `1000`.
|
2021-01-15 02:17:56 -08:00
|
|
|
|
* `maxSockets` {number} Maximum number of sockets to allow per host.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
If the same host opens multiple concurrent connections, each request
|
|
|
|
|
will use new socket until the `maxSockets` value is reached.
|
|
|
|
|
If the host attempts to open more connections than `maxSockets`,
|
|
|
|
|
the additional requests will enter into a pending request queue, and
|
|
|
|
|
will enter active connection state when an existing connection terminates.
|
|
|
|
|
This makes sure there are at most `maxSockets` active connections at
|
|
|
|
|
any point in time, from a given host.
|
2019-03-03 12:28:17 +01:00
|
|
|
|
**Default:** `Infinity`.
|
2020-06-22 21:15:34 +08:00
|
|
|
|
* `maxTotalSockets` {number} Maximum number of sockets allowed for
|
|
|
|
|
all hosts in total. Each request will use a new socket
|
|
|
|
|
until the maximum is reached.
|
|
|
|
|
**Default:** `Infinity`.
|
2021-10-16 19:55:31 +02:00
|
|
|
|
* `maxFreeSockets` {number} Maximum number of sockets per host to leave open
|
2018-04-02 08:38:48 +03:00
|
|
|
|
in a free state. Only relevant if `keepAlive` is set to `true`.
|
2018-04-02 04:44:32 +03:00
|
|
|
|
**Default:** `256`.
|
2020-05-07 08:18:11 +02:00
|
|
|
|
* `scheduling` {string} Scheduling strategy to apply when picking
|
|
|
|
|
the next free socket to use. It can be `'fifo'` or `'lifo'`.
|
|
|
|
|
The main difference between the two scheduling strategies is that `'lifo'`
|
|
|
|
|
selects the most recently used socket, while `'fifo'` selects
|
|
|
|
|
the least recently used socket.
|
|
|
|
|
In case of a low rate of request per second, the `'lifo'` scheduling
|
|
|
|
|
will lower the risk of picking a socket that might have been closed
|
|
|
|
|
by the server due to inactivity.
|
|
|
|
|
In case of a high rate of request per second,
|
|
|
|
|
the `'fifo'` scheduling will maximize the number of open sockets,
|
|
|
|
|
while the `'lifo'` scheduling will keep it as low as possible.
|
2020-12-30 13:02:27 +01:00
|
|
|
|
**Default:** `'lifo'`.
|
2018-06-08 12:42:27 +08:00
|
|
|
|
* `timeout` {number} Socket timeout in milliseconds.
|
2019-01-14 10:20:34 +01:00
|
|
|
|
This will set the timeout when the socket is created.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-12-05 01:41:19 -05:00
|
|
|
|
`options` in [`socket.connect()`][] are also supported.
|
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
To configure any of them, a custom [`http.Agent`][] instance must be created.
|
2012-06-12 10:02:52 -07:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import { Agent, request } from 'node:http';
|
|
|
|
|
const keepAliveAgent = new Agent({ keepAlive: true });
|
|
|
|
|
options.agent = keepAliveAgent;
|
|
|
|
|
request(options, onResponseCallback);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const keepAliveAgent = new http.Agent({ keepAlive: true });
|
2015-11-04 17:56:03 -05:00
|
|
|
|
options.agent = keepAliveAgent;
|
|
|
|
|
http.request(options, onResponseCallback);
|
|
|
|
|
```
|
2012-06-12 10:02:52 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.createConnection(options[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.4
|
|
|
|
|
-->
|
2016-01-11 23:41:01 -05:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `options` {Object} Options containing connection details. Check
|
|
|
|
|
[`net.createConnection()`][] for the format of the options
|
|
|
|
|
* `callback` {Function} Callback function that receives the created socket
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* Returns: {stream.Duplex}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2016-01-11 23:41:01 -05:00
|
|
|
|
Produces a socket/stream to be used for HTTP requests.
|
|
|
|
|
|
|
|
|
|
By default, this function is the same as [`net.createConnection()`][]. However,
|
2017-01-09 12:45:46 -05:00
|
|
|
|
custom agents may override this method in case greater flexibility is desired.
|
2016-01-11 23:41:01 -05:00
|
|
|
|
|
|
|
|
|
A socket/stream can be supplied in one of two ways: by returning the
|
|
|
|
|
socket/stream from this function, or by passing the socket/stream to `callback`.
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This method is guaranteed to return an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2016-01-11 23:41:01 -05:00
|
|
|
|
`callback` has a signature of `(err, stream)`.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.keepSocketAlive(socket)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-05-12 16:53:15 -04:00
|
|
|
|
<!-- YAML
|
2017-11-16 22:32:56 +08:00
|
|
|
|
added: v8.1.0
|
2017-05-12 16:53:15 -04:00
|
|
|
|
-->
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2017-05-12 16:53:15 -04:00
|
|
|
|
|
|
|
|
|
Called when `socket` is detached from a request and could be persisted by the
|
2018-04-29 20:46:41 +03:00
|
|
|
|
`Agent`. Default behavior is to:
|
2017-05-12 16:53:15 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2017-08-04 17:56:03 +08:00
|
|
|
|
socket.setKeepAlive(true, this.keepAliveMsecs);
|
2017-05-12 16:53:15 -04:00
|
|
|
|
socket.unref();
|
2017-08-04 17:56:03 +08:00
|
|
|
|
return true;
|
2017-05-12 16:53:15 -04:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This method can be overridden by a particular `Agent` subclass. If this
|
|
|
|
|
method returns a falsy value, the socket will be destroyed instead of persisting
|
|
|
|
|
it for use with the next request.
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
The `socket` argument can be an instance of {net.Socket}, a subclass of
|
|
|
|
|
{stream.Duplex}.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.reuseSocket(socket, request)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-05-12 16:53:15 -04:00
|
|
|
|
<!-- YAML
|
2017-11-16 22:32:56 +08:00
|
|
|
|
added: v8.1.0
|
2017-05-12 16:53:15 -04:00
|
|
|
|
-->
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2017-05-12 16:53:15 -04:00
|
|
|
|
* `request` {http.ClientRequest}
|
|
|
|
|
|
|
|
|
|
Called when `socket` is attached to `request` after being persisted because of
|
|
|
|
|
the keep-alive options. Default behavior is to:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
socket.ref();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This method can be overridden by a particular `Agent` subclass.
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
The `socket` argument can be an instance of {net.Socket}, a subclass of
|
|
|
|
|
{stream.Duplex}.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.destroy()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.4
|
|
|
|
|
-->
|
2012-06-12 10:02:52 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Destroy any sockets that are currently in use by the agent.
|
2012-06-12 10:02:52 -07:00
|
|
|
|
|
2018-04-02 08:38:48 +03:00
|
|
|
|
It is usually not necessary to do this. However, if using an
|
2017-01-09 12:45:46 -05:00
|
|
|
|
agent with `keepAlive` enabled, then it is best to explicitly shut down
|
2020-11-18 06:53:25 -08:00
|
|
|
|
the agent when it is no longer needed. Otherwise,
|
|
|
|
|
sockets might stay open for quite a long time before the server
|
2015-11-04 17:56:03 -05:00
|
|
|
|
terminates them.
|
2012-06-12 10:02:52 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.freeSockets`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.4
|
2020-12-06 14:52:08 +01:00
|
|
|
|
changes:
|
2021-03-03 15:36:13 +00:00
|
|
|
|
- version: v16.0.0
|
2020-12-06 14:52:08 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/36409
|
|
|
|
|
description: The property now has a `null` prototype.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* {Object}
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
An object which contains arrays of sockets currently awaiting use by
|
2018-04-02 08:38:48 +03:00
|
|
|
|
the agent when `keepAlive` is enabled. Do not modify.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2020-02-28 22:27:39 +01:00
|
|
|
|
Sockets in the `freeSockets` list will be automatically destroyed and
|
|
|
|
|
removed from the array on `'timeout'`.
|
|
|
|
|
|
2022-02-27 23:01:01 +08:00
|
|
|
|
### `agent.getName([options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.4
|
2022-02-27 23:01:01 +08:00
|
|
|
|
changes:
|
2022-04-23 21:03:46 -04:00
|
|
|
|
- version:
|
|
|
|
|
- v17.7.0
|
|
|
|
|
- v16.15.0
|
2022-02-27 23:01:01 +08:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/41906
|
|
|
|
|
description: The `options` parameter is now optional.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `options` {Object} A set of options providing information for name generation
|
2018-02-12 02:31:55 -05:00
|
|
|
|
* `host` {string} A domain name or IP address of the server to issue the
|
|
|
|
|
request to
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `port` {number} Port of remote server
|
|
|
|
|
* `localAddress` {string} Local interface to bind for network connections
|
2016-09-10 16:03:30 -07:00
|
|
|
|
when issuing the request
|
2017-08-04 17:56:03 +08:00
|
|
|
|
* `family` {integer} Must be 4 or 6 if this doesn't equal `undefined`.
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* Returns: {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Get a unique name for a set of request options, to determine whether a
|
2017-08-04 17:56:03 +08:00
|
|
|
|
connection can be reused. For an HTTP agent, this returns
|
|
|
|
|
`host:port:localAddress` or `host:port:localAddress:family`. For an HTTPS agent,
|
|
|
|
|
the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
|
|
|
|
|
that determine socket reusability.
|
2012-01-16 01:45:31 +06:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.maxFreeSockets`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.7
|
|
|
|
|
-->
|
2012-01-16 01:45:31 +06:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {number}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2018-04-02 08:38:48 +03:00
|
|
|
|
By default set to 256. For agents with `keepAlive` enabled, this
|
2015-11-04 17:56:03 -05:00
|
|
|
|
sets the maximum number of sockets that will be left open in the free
|
|
|
|
|
state.
|
2012-01-16 01:45:31 +06:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.maxSockets`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
|
|
|
|
-->
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {number}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2018-04-29 20:46:41 +03:00
|
|
|
|
By default set to `Infinity`. Determines how many concurrent sockets the agent
|
2017-08-04 17:56:03 +08:00
|
|
|
|
can have open per origin. Origin is the returned value of [`agent.getName()`][].
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2020-05-28 23:15:55 +08:00
|
|
|
|
### `agent.maxTotalSockets`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-05-28 23:15:55 +08:00
|
|
|
|
<!-- YAML
|
2020-09-28 10:54:13 -07:00
|
|
|
|
added:
|
|
|
|
|
- v14.5.0
|
|
|
|
|
- v12.19.0
|
2020-05-28 23:15:55 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {number}
|
|
|
|
|
|
|
|
|
|
By default set to `Infinity`. Determines how many concurrent sockets the agent
|
|
|
|
|
can have open. Unlike `maxSockets`, this parameter applies across all origins.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.requests`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
2020-12-06 14:52:08 +01:00
|
|
|
|
changes:
|
2021-03-03 15:36:13 +00:00
|
|
|
|
- version: v16.0.0
|
2020-12-06 14:52:08 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/36409
|
|
|
|
|
description: The property now has a `null` prototype.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* {Object}
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
An object which contains queues of requests that have not yet been assigned to
|
|
|
|
|
sockets. Do not modify.
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `agent.sockets`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
2020-12-06 14:52:08 +01:00
|
|
|
|
changes:
|
2021-03-03 15:36:13 +00:00
|
|
|
|
- version: v16.0.0
|
2020-12-06 14:52:08 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/36409
|
|
|
|
|
description: The property now has a `null` prototype.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2015-05-13 21:25:57 -05:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* {Object}
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
An object which contains arrays of sockets currently in use by the
|
2018-04-02 08:38:48 +03:00
|
|
|
|
agent. Do not modify.
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## Class: `http.ClientRequest`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.17
|
|
|
|
|
-->
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2022-04-07 22:18:43 +05:30
|
|
|
|
* Extends: {http.OutgoingMessage}
|
2019-08-21 13:11:16 -07:00
|
|
|
|
|
2018-04-02 08:38:48 +03:00
|
|
|
|
This object is created internally and returned from [`http.request()`][]. It
|
|
|
|
|
represents an _in-progress_ request whose header has already been queued. The
|
2017-09-01 15:28:12 +02:00
|
|
|
|
header is still mutable using the [`setHeader(name, value)`][],
|
2021-10-10 21:55:04 -07:00
|
|
|
|
[`getHeader(name)`][], [`removeHeader(name)`][] API. The actual header will
|
2017-09-01 15:28:12 +02:00
|
|
|
|
be sent along with the first data chunk or when calling [`request.end()`][].
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2016-07-09 08:13:09 +03:00
|
|
|
|
To get the response, add a listener for [`'response'`][] to the request object.
|
|
|
|
|
[`'response'`][] will be emitted from the request object when the response
|
2018-04-02 08:38:48 +03:00
|
|
|
|
headers have been received. The [`'response'`][] event is executed with one
|
2015-11-27 18:30:32 -05:00
|
|
|
|
argument which is an instance of [`http.IncomingMessage`][].
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2016-07-09 08:13:09 +03:00
|
|
|
|
During the [`'response'`][] event, one can add listeners to the
|
2015-11-04 17:56:03 -05:00
|
|
|
|
response object; particularly to listen for the `'data'` event.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-07-09 08:13:09 +03:00
|
|
|
|
If no [`'response'`][] handler is added, then the response will be
|
2018-04-02 08:38:48 +03:00
|
|
|
|
entirely discarded. However, if a [`'response'`][] event handler is added,
|
2017-03-03 15:45:20 -08:00
|
|
|
|
then the data from the response object **must** be consumed, either by
|
2015-11-04 17:56:03 -05:00
|
|
|
|
calling `response.read()` whenever there is a `'readable'` event, or
|
|
|
|
|
by adding a `'data'` handler, or by calling the `.resume()` method.
|
2018-04-02 08:38:48 +03:00
|
|
|
|
Until the data is consumed, the `'end'` event will not fire. Also, until
|
2015-11-04 17:56:03 -05:00
|
|
|
|
the data is read it will consume memory that can eventually lead to a
|
|
|
|
|
'process out of memory' error.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2020-04-30 20:29:35 +02:00
|
|
|
|
For backward compatibility, `res` will only emit `'error'` if there is an
|
|
|
|
|
`'error'` listener registered.
|
2019-06-17 06:26:52 -02:00
|
|
|
|
|
2023-02-14 20:55:52 +01:00
|
|
|
|
Set `Content-Length` header to limit the response body size.
|
|
|
|
|
If [`response.strictContentLength`][] is set to `true`, mismatching the
|
|
|
|
|
`Content-Length` header value will result in an `Error` being thrown,
|
2022-08-24 22:19:29 +05:30
|
|
|
|
identified by `code:` [`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`][].
|
|
|
|
|
|
|
|
|
|
`Content-Length` value should be in bytes, not characters. Use
|
|
|
|
|
[`Buffer.byteLength()`][] to determine the length of the body in bytes.
|
2012-02-05 19:11:54 +09:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'abort'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.4.1
|
2021-10-18 12:00:42 -04:00
|
|
|
|
deprecated:
|
|
|
|
|
- v17.0.0
|
|
|
|
|
- v16.12.0
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2013-11-26 12:49:28 -08:00
|
|
|
|
|
2021-01-19 07:55:57 +08:00
|
|
|
|
> Stability: 0 - Deprecated. Listen for the `'close'` event instead.
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Emitted when the request has been aborted by the client. This event is only
|
|
|
|
|
emitted on the first call to `abort()`.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2022-04-15 00:09:19 +02:00
|
|
|
|
### Event: `'close'`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.4
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Indicates that the request is completed, or its underlying connection was
|
|
|
|
|
terminated prematurely (before the response completion).
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'connect'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `response` {http.IncomingMessage}
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `head` {Buffer}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
Emitted each time a server responds to a request with a `CONNECT` method. If
|
|
|
|
|
this event is not being listened for, clients receiving a `CONNECT` method will
|
|
|
|
|
have their connections closed.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
A client and server pair demonstrating how to listen for the `'connect'` event:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import { createServer, request } from 'node:http';
|
|
|
|
|
import { connect } from 'node:net';
|
|
|
|
|
import { URL } from 'node:url';
|
|
|
|
|
|
|
|
|
|
// Create an HTTP tunneling proxy
|
|
|
|
|
const proxy = createServer((req, res) => {
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
|
|
|
res.end('okay');
|
|
|
|
|
});
|
|
|
|
|
proxy.on('connect', (req, clientSocket, head) => {
|
|
|
|
|
// Connect to an origin server
|
|
|
|
|
const { port, hostname } = new URL(`http://${req.url}`);
|
|
|
|
|
const serverSocket = connect(port || 80, hostname, () => {
|
|
|
|
|
clientSocket.write('HTTP/1.1 200 Connection Established\r\n' +
|
|
|
|
|
'Proxy-agent: Node.js-Proxy\r\n' +
|
|
|
|
|
'\r\n');
|
|
|
|
|
serverSocket.write(head);
|
|
|
|
|
serverSocket.pipe(clientSocket);
|
|
|
|
|
clientSocket.pipe(serverSocket);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Now that proxy is running
|
|
|
|
|
proxy.listen(1337, '127.0.0.1', () => {
|
|
|
|
|
|
|
|
|
|
// Make a request to a tunneling proxy
|
|
|
|
|
const options = {
|
|
|
|
|
port: 1337,
|
|
|
|
|
host: '127.0.0.1',
|
|
|
|
|
method: 'CONNECT',
|
|
|
|
|
path: 'www.google.com:80',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
|
|
req.on('connect', (res, socket, head) => {
|
|
|
|
|
console.log('got connected!');
|
|
|
|
|
|
|
|
|
|
// Make a request over an HTTP tunnel
|
|
|
|
|
socket.write('GET / HTTP/1.1\r\n' +
|
|
|
|
|
'Host: www.google.com:80\r\n' +
|
|
|
|
|
'Connection: close\r\n' +
|
|
|
|
|
'\r\n');
|
|
|
|
|
socket.on('data', (chunk) => {
|
|
|
|
|
console.log(chunk.toString());
|
|
|
|
|
});
|
|
|
|
|
socket.on('end', () => {
|
|
|
|
|
proxy.close();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
|
|
|
|
const net = require('node:net');
|
|
|
|
|
const { URL } = require('node:url');
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
|
|
// Create an HTTP tunneling proxy
|
2017-04-21 17:38:31 +03:00
|
|
|
|
const proxy = http.createServer((req, res) => {
|
2017-06-01 02:07:25 +03:00
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-01-17 18:39:07 +01:00
|
|
|
|
res.end('okay');
|
|
|
|
|
});
|
2020-01-07 10:11:11 +00:00
|
|
|
|
proxy.on('connect', (req, clientSocket, head) => {
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// Connect to an origin server
|
2019-10-10 11:05:48 +02:00
|
|
|
|
const { port, hostname } = new URL(`http://${req.url}`);
|
2020-01-07 10:11:11 +00:00
|
|
|
|
const serverSocket = net.connect(port || 80, hostname, () => {
|
|
|
|
|
clientSocket.write('HTTP/1.1 200 Connection Established\r\n' +
|
2016-01-17 18:39:07 +01:00
|
|
|
|
'Proxy-agent: Node.js-Proxy\r\n' +
|
|
|
|
|
'\r\n');
|
2020-01-07 10:11:11 +00:00
|
|
|
|
serverSocket.write(head);
|
|
|
|
|
serverSocket.pipe(clientSocket);
|
|
|
|
|
clientSocket.pipe(serverSocket);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// Now that proxy is running
|
2016-01-17 18:39:07 +01:00
|
|
|
|
proxy.listen(1337, '127.0.0.1', () => {
|
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Make a request to a tunneling proxy
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
port: 1337,
|
2018-11-06 09:58:42 -08:00
|
|
|
|
host: '127.0.0.1',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
method: 'CONNECT',
|
2022-11-17 08:19:12 -05:00
|
|
|
|
path: 'www.google.com:80',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
};
|
|
|
|
|
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const req = http.request(options);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
|
|
req.on('connect', (res, socket, head) => {
|
|
|
|
|
console.log('got connected!');
|
|
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Make a request over an HTTP tunnel
|
2016-01-17 18:39:07 +01:00
|
|
|
|
socket.write('GET / HTTP/1.1\r\n' +
|
|
|
|
|
'Host: www.google.com:80\r\n' +
|
|
|
|
|
'Connection: close\r\n' +
|
|
|
|
|
'\r\n');
|
|
|
|
|
socket.on('data', (chunk) => {
|
|
|
|
|
console.log(chunk.toString());
|
2015-11-04 17:56:03 -05:00
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
socket.on('end', () => {
|
|
|
|
|
proxy.close();
|
2015-11-04 17:56:03 -05:00
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'continue'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.2
|
|
|
|
|
-->
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Emitted when the server sends a '100 Continue' HTTP response, usually because
|
|
|
|
|
the request contained 'Expect: 100-continue'. This is an instruction that
|
|
|
|
|
the client should send the request body.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2022-04-15 00:09:19 +02:00
|
|
|
|
### Event: `'finish'`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Emitted when the request has been sent. More specifically, this event is emitted
|
|
|
|
|
when the last segment of the response headers and body have been handed off to
|
|
|
|
|
the operating system for transmission over the network. It does not imply that
|
|
|
|
|
the server has received anything yet.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'information'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-01-08 00:11:05 -08:00
|
|
|
|
<!-- YAML
|
2018-03-02 09:53:46 -08:00
|
|
|
|
added: v10.0.0
|
2018-01-08 00:11:05 -08:00
|
|
|
|
-->
|
|
|
|
|
|
2019-03-30 09:55:28 +01:00
|
|
|
|
* `info` {Object}
|
2019-06-27 16:10:55 -07:00
|
|
|
|
* `httpVersion` {string}
|
|
|
|
|
* `httpVersionMajor` {integer}
|
|
|
|
|
* `httpVersionMinor` {integer}
|
2019-03-30 09:55:28 +01:00
|
|
|
|
* `statusCode` {integer}
|
2019-06-27 16:10:55 -07:00
|
|
|
|
* `statusMessage` {string}
|
|
|
|
|
* `headers` {Object}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
* `rawHeaders` {string\[]}
|
2019-06-27 16:10:55 -07:00
|
|
|
|
|
|
|
|
|
Emitted when the server sends a 1xx intermediate response (excluding 101
|
|
|
|
|
Upgrade). The listeners of this event will receive an object containing the
|
|
|
|
|
HTTP version, status code, status message, key-value headers object,
|
|
|
|
|
and array with the raw header names followed by their respective values.
|
2018-01-08 00:11:05 -08:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import { request } from 'node:http';
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
host: '127.0.0.1',
|
|
|
|
|
port: 8080,
|
|
|
|
|
path: '/length_request',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Make a request
|
|
|
|
|
const req = request(options);
|
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
|
|
req.on('information', (info) => {
|
|
|
|
|
console.log(`Got information prior to main response: ${info.statusCode}`);
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2018-01-08 00:11:05 -08:00
|
|
|
|
|
|
|
|
|
const options = {
|
2018-11-06 09:58:42 -08:00
|
|
|
|
host: '127.0.0.1',
|
2018-01-08 00:11:05 -08:00
|
|
|
|
port: 8080,
|
2022-11-17 08:19:12 -05:00
|
|
|
|
path: '/length_request',
|
2018-01-08 00:11:05 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Make a request
|
|
|
|
|
const req = http.request(options);
|
|
|
|
|
req.end();
|
|
|
|
|
|
2019-03-30 09:55:28 +01:00
|
|
|
|
req.on('information', (info) => {
|
|
|
|
|
console.log(`Got information prior to main response: ${info.statusCode}`);
|
2018-01-08 00:11:05 -08:00
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
101 Upgrade statuses do not fire this event due to their break from the
|
|
|
|
|
traditional HTTP request/response chain, such as web sockets, in-place TLS
|
|
|
|
|
upgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the
|
|
|
|
|
[`'upgrade'`][] event instead.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'response'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.0
|
|
|
|
|
-->
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `response` {http.IncomingMessage}
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Emitted when a response is received to this request. This event is emitted only
|
2016-09-10 16:03:30 -07:00
|
|
|
|
once.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'socket'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.3
|
|
|
|
|
-->
|
2013-10-17 02:11:19 +02:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2013-10-17 02:11:19 +02:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
2013-10-17 02:11:19 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'timeout'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-09-17 09:30:16 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.8
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Emitted when the underlying socket times out from inactivity. This only notifies
|
2021-01-19 07:55:57 +08:00
|
|
|
|
that the socket has been idle. The request must be destroyed manually.
|
2017-09-17 09:30:16 +02:00
|
|
|
|
|
2018-04-29 14:16:44 +03:00
|
|
|
|
See also: [`request.setTimeout()`][].
|
2017-09-17 09:30:16 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'upgrade'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.94
|
|
|
|
|
-->
|
2013-10-17 02:11:19 +02:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `response` {http.IncomingMessage}
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `head` {Buffer}
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Emitted each time a server responds to a request with an upgrade. If this
|
2018-04-12 19:57:19 +02:00
|
|
|
|
event is not being listened for and the response status code is 101 Switching
|
|
|
|
|
Protocols, clients receiving an upgrade header will have their connections
|
|
|
|
|
closed.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
A client server pair demonstrating how to listen for the `'upgrade'` event.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
|
|
// Create an HTTP server
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
|
|
|
res.end('okay');
|
|
|
|
|
});
|
|
|
|
|
server.on('upgrade', (req, socket, head) => {
|
|
|
|
|
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
|
'\r\n');
|
|
|
|
|
|
|
|
|
|
socket.pipe(socket); // echo back
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Now that server is running
|
|
|
|
|
server.listen(1337, '127.0.0.1', () => {
|
|
|
|
|
|
|
|
|
|
// make a request
|
|
|
|
|
const options = {
|
|
|
|
|
port: 1337,
|
|
|
|
|
host: '127.0.0.1',
|
|
|
|
|
headers: {
|
|
|
|
|
'Connection': 'Upgrade',
|
|
|
|
|
'Upgrade': 'websocket',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const req = http.request(options);
|
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
|
|
req.on('upgrade', (res, socket, upgradeHead) => {
|
|
|
|
|
console.log('got upgraded!');
|
|
|
|
|
socket.end();
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// Create an HTTP server
|
2020-01-07 10:11:11 +00:00
|
|
|
|
const server = http.createServer((req, res) => {
|
2017-06-01 02:07:25 +03:00
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-01-17 18:39:07 +01:00
|
|
|
|
res.end('okay');
|
|
|
|
|
});
|
2020-01-07 10:11:11 +00:00
|
|
|
|
server.on('upgrade', (req, socket, head) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
|
'\r\n');
|
|
|
|
|
|
|
|
|
|
socket.pipe(socket); // echo back
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// Now that server is running
|
2020-01-07 10:11:11 +00:00
|
|
|
|
server.listen(1337, '127.0.0.1', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
|
|
// make a request
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
port: 1337,
|
2018-11-06 09:58:42 -08:00
|
|
|
|
host: '127.0.0.1',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
headers: {
|
|
|
|
|
'Connection': 'Upgrade',
|
2022-11-17 08:19:12 -05:00
|
|
|
|
'Upgrade': 'websocket',
|
|
|
|
|
},
|
2016-01-17 18:39:07 +01:00
|
|
|
|
};
|
|
|
|
|
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const req = http.request(options);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
|
|
req.on('upgrade', (res, socket, upgradeHead) => {
|
|
|
|
|
console.log('got upgraded!');
|
|
|
|
|
socket.end();
|
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
2012-02-15 07:38:24 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.abort()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.8
|
2020-04-28 13:54:04 +02:00
|
|
|
|
deprecated:
|
|
|
|
|
- v14.1.0
|
|
|
|
|
- v13.14.0
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2012-02-15 07:38:24 +11:00
|
|
|
|
|
2020-05-01 23:34:22 +02:00
|
|
|
|
> Stability: 0 - Deprecated: Use [`request.destroy()`][] instead.
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Marks the request as aborting. Calling this will cause remaining data
|
2019-07-15 07:57:40 -07:00
|
|
|
|
in the response to be dropped and the socket to be destroyed.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.aborted`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-02-24 12:47:18 -08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.14
|
2021-10-18 12:00:42 -04:00
|
|
|
|
deprecated:
|
|
|
|
|
- v17.0.0
|
|
|
|
|
- v16.12.0
|
2018-04-26 09:53:54 +02:00
|
|
|
|
changes:
|
2018-10-02 16:01:19 -07:00
|
|
|
|
- version: v11.0.0
|
2018-04-26 09:53:54 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/20230
|
|
|
|
|
description: The `aborted` property is no longer a timestamp number.
|
2017-02-24 12:47:18 -08:00
|
|
|
|
-->
|
|
|
|
|
|
2021-01-19 07:55:57 +08:00
|
|
|
|
> Stability: 0 - Deprecated. Check [`request.destroyed`][] instead.
|
|
|
|
|
|
2018-04-26 09:53:54 +02:00
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
The `request.aborted` property will be `true` if the request has
|
|
|
|
|
been aborted.
|
2017-02-24 12:47:18 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.connection`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-06-11 11:18:03 -07:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
2019-09-09 13:32:18 +01:00
|
|
|
|
deprecated: v13.0.0
|
2017-06-11 11:18:03 -07:00
|
|
|
|
-->
|
|
|
|
|
|
2019-08-06 13:56:52 +02:00
|
|
|
|
> Stability: 0 - Deprecated. Use [`request.socket`][].
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* {stream.Duplex}
|
2017-06-11 11:18:03 -07:00
|
|
|
|
|
2018-04-29 14:16:44 +03:00
|
|
|
|
See [`request.socket`][].
|
2017-06-11 11:18:03 -07:00
|
|
|
|
|
2022-04-18 20:49:05 +02:00
|
|
|
|
### `request.cork()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
See [`writable.cork()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.end([data[, encoding]][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
2018-02-14 12:32:01 +00:00
|
|
|
|
changes:
|
2022-10-28 01:45:05 +02:00
|
|
|
|
- version: v15.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33155
|
|
|
|
|
description: The `data` parameter can now be a `Uint8Array`.
|
2018-03-02 09:53:46 -08:00
|
|
|
|
- version: v10.0.0
|
2018-02-14 12:32:01 +00:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/18780
|
|
|
|
|
description: This method now returns a reference to `ClientRequest`.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2022-10-28 01:45:05 +02:00
|
|
|
|
* `data` {string|Buffer|Uint8Array}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `encoding` {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
2018-02-14 12:32:01 +00:00
|
|
|
|
* Returns: {this}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Finishes sending the request. If any parts of the body are
|
|
|
|
|
unsent, it will flush them to the stream. If the request is
|
|
|
|
|
chunked, this will send the terminating `'0\r\n\r\n'`.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
If `data` is specified, it is equivalent to calling
|
2017-07-08 00:05:08 +08:00
|
|
|
|
[`request.write(data, encoding)`][] followed by `request.end(callback)`.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
If `callback` is specified, it will be called when the request stream
|
|
|
|
|
is finished.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2020-04-13 11:02:03 +02:00
|
|
|
|
### `request.destroy([error])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-04-13 11:02:03 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
2020-05-08 21:25:55 -04:00
|
|
|
|
changes:
|
2020-06-27 20:26:32 -07:00
|
|
|
|
- version: v14.5.0
|
2020-05-08 21:25:55 -04:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/32789
|
|
|
|
|
description: The function returns `this` for consistency with other Readable
|
|
|
|
|
streams.
|
2020-04-13 11:02:03 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `error` {Error} Optional, an error to emit with `'error'` event.
|
|
|
|
|
* Returns: {this}
|
|
|
|
|
|
|
|
|
|
Destroy the request. Optionally emit an `'error'` event,
|
|
|
|
|
and emit a `'close'` event. Calling this will cause remaining data
|
|
|
|
|
in the response to be dropped and the socket to be destroyed.
|
|
|
|
|
|
|
|
|
|
See [`writable.destroy()`][] for further details.
|
|
|
|
|
|
|
|
|
|
#### `request.destroyed`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-04-13 11:02:03 +02:00
|
|
|
|
<!-- YAML
|
2020-04-28 13:54:04 +02:00
|
|
|
|
added:
|
|
|
|
|
- v14.1.0
|
|
|
|
|
- v13.14.0
|
2020-04-13 11:02:03 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
Is `true` after [`request.destroy()`][] has been called.
|
|
|
|
|
|
|
|
|
|
See [`writable.destroyed`][] for further details.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.finished`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-11-12 12:36:32 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.1
|
2020-04-24 18:43:06 +02:00
|
|
|
|
deprecated:
|
|
|
|
|
- v13.4.0
|
|
|
|
|
- v12.16.0
|
2018-11-12 12:36:32 +01:00
|
|
|
|
-->
|
|
|
|
|
|
2019-07-14 16:59:25 +02:00
|
|
|
|
> Stability: 0 - Deprecated. Use [`request.writableEnded`][].
|
|
|
|
|
|
2018-11-12 12:36:32 +01:00
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
The `request.finished` property will be `true` if [`request.end()`][]
|
|
|
|
|
has been called. `request.end()` will automatically be called if the
|
|
|
|
|
request was initiated via [`http.get()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.flushHeaders()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.6.0
|
|
|
|
|
-->
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2019-07-22 17:30:20 +02:00
|
|
|
|
Flushes the request headers.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
For efficiency reasons, Node.js normally buffers the request headers until
|
|
|
|
|
`request.end()` is called or the first chunk of request data is written. It
|
|
|
|
|
then tries to pack the request headers and data into a single TCP packet.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
That's usually desired (it saves a TCP round-trip), but not when the first
|
2018-04-02 08:38:48 +03:00
|
|
|
|
data is not sent until possibly much later. `request.flushHeaders()` bypasses
|
2017-03-03 15:45:20 -08:00
|
|
|
|
the optimization and kickstarts the request.
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.getHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-09-01 15:28:12 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.6.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
2018-04-09 22:43:37 +02:00
|
|
|
|
* Returns: {any}
|
2017-09-01 15:28:12 +02:00
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
Reads out a header on the request. The name is case-insensitive.
|
2018-04-09 22:43:37 +02:00
|
|
|
|
The type of the return value depends on the arguments provided to
|
|
|
|
|
[`request.setHeader()`][].
|
2017-09-01 15:28:12 +02:00
|
|
|
|
|
|
|
|
|
```js
|
2018-04-09 22:43:37 +02:00
|
|
|
|
request.setHeader('content-type', 'text/html');
|
|
|
|
|
request.setHeader('Content-Length', Buffer.byteLength(body));
|
2018-10-17 09:40:50 +02:00
|
|
|
|
request.setHeader('Cookie', ['type=ninja', 'language=javascript']);
|
2017-09-01 15:28:12 +02:00
|
|
|
|
const contentType = request.getHeader('Content-Type');
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// 'contentType' is 'text/html'
|
2018-04-09 22:43:37 +02:00
|
|
|
|
const contentLength = request.getHeader('Content-Length');
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// 'contentLength' is of type number
|
2018-10-17 09:40:50 +02:00
|
|
|
|
const cookie = request.getHeader('Cookie');
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// 'cookie' is of type string[]
|
2017-09-01 15:28:12 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-04-18 20:49:05 +02:00
|
|
|
|
### `request.getHeaderNames()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v7.7.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {string\[]}
|
|
|
|
|
|
|
|
|
|
Returns an array containing the unique names of the current outgoing headers.
|
|
|
|
|
All header names are lowercase.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
request.setHeader('Foo', 'bar');
|
|
|
|
|
request.setHeader('Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
|
|
|
|
|
const headerNames = request.getHeaderNames();
|
2022-07-24 00:50:39 +08:00
|
|
|
|
// headerNames === ['foo', 'cookie']
|
2022-04-18 20:49:05 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `request.getHeaders()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v7.7.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {Object}
|
|
|
|
|
|
|
|
|
|
Returns a shallow copy of the current outgoing headers. Since a shallow copy
|
|
|
|
|
is used, array values may be mutated without additional calls to various
|
|
|
|
|
header-related http module methods. The keys of the returned object are the
|
|
|
|
|
header names and the values are the respective header values. All header names
|
|
|
|
|
are lowercase.
|
|
|
|
|
|
2022-09-12 21:29:30 +02:00
|
|
|
|
The object returned by the `request.getHeaders()` method _does not_
|
2022-04-18 20:49:05 +02:00
|
|
|
|
prototypically inherit from the JavaScript `Object`. This means that typical
|
|
|
|
|
`Object` methods such as `obj.toString()`, `obj.hasOwnProperty()`, and others
|
|
|
|
|
are not defined and _will not work_.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
request.setHeader('Foo', 'bar');
|
|
|
|
|
request.setHeader('Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
|
2022-09-12 21:29:30 +02:00
|
|
|
|
const headers = request.getHeaders();
|
2022-04-18 20:49:05 +02:00
|
|
|
|
// headers === { foo: 'bar', 'cookie': ['foo=bar', 'bar=baz'] }
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-08 21:27:49 +02:00
|
|
|
|
### `request.getRawHeaderNames()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-03-08 21:27:49 +02:00
|
|
|
|
<!-- YAML
|
2021-05-11, Version 14.17.0 'Fermium' (LTS)
Notable Changes:
Diagnostics channel (experimental module):
`diagnostics_channel` is a new experimental module that provides an API
to create named channels to report arbitrary message data for
diagnostics purposes.
The module was initially introduced in Node.js v15.1.0 and is
backported to v14.17.0 to enable testing it at a larger scale.
With `diagnostics_channel`, Node.js core and module authors can publish
contextual data about what they are doing at a given time. This could
be the hostname and query string of a mysql query, for example. Just
create a named channel with `dc.channel(name)` and call
`channel.publish(data)` to send the data to any listeners to that
channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
MySQL.prototype.query = function query(queryString, values, callback) {
// Broadcast query information whenever a query is made
channel.publish({
query: queryString,
host: this.hostname,
});
this.doQuery(queryString, values, callback);
};
```
Channels are like one big global event emitter but are split into
separate objects to ensure they get the best performance. If nothing is
listening to the channel, the publishing overhead should be as close to
zero as possible. Consuming channel data is as easy as using
`channel.subscribe(listener)` to run a function whenever a message is
published to that channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
channel.subscribe(({ query, host }) => {
console.log(`mysql query to ${host}: ${query}`);
});
```
The data captured can be used to provide context for what an app is
doing at a given time. This can be used for things like augmenting
tracing data, tracking network and filesystem activity, logging
queries, and many other things. It's also a very useful data source
for diagnostics tools to provide a clearer picture of exactly what the
application is doing at a given point in the data they are presenting.
Contributed by Stephen Belanger (https://github.com/nodejs/node/pull/34895).
UUID support in the crypto module:
The new `crypto.randomUUID()` method now allows to generate random
[RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4
UUID strings:
```js
const { randomUUID } = require('crypto');
console.log(randomUUID());
// 'aa7c91a1-f8fc-4339-b9db-f93fc7233429'
```
Contributed by James M Snell (https://github.com/nodejs/node/pull/36729).
Experimental support for `AbortController` and `AbortSignal`:
Node.js 14.17.0 adds experimental partial support for `AbortController`
and `AbortSignal`.
Both constructors can be enabled globally using the
`--experimental-abortcontroller` flag.
Additionally, several Node.js APIs have been updated to support
`AbortSignal` for cancellation.
It is not mandatory to use the built-in constructors with them. Any
spec-compliant third-party alternatives should be compatible.
`AbortSignal` support was added to the following methods:
* `child_process.exec`
* `child_process.execFile`
* `child_process.fork`
* `child_process.spawn`
* `dgram.createSocket`
* `events.on`
* `events.once`
* `fs.readFile`
* `fs.watch`
* `fs.writeFile`
* `http.request`
* `https.request`
* `http2Session.request`
* The promisified variants of `setImmediate` and `setTimeout`
Other notable changes:
* doc:
* revoke deprecation of legacy url, change status to legacy (James M Snell) (https://github.com/nodejs/node/pull/37784)
* add legacy status to stability index (James M Snell) (https://github.com/nodejs/node/pull/37784)
* upgrade stability status of report API (Gireesh Punathil) (https://github.com/nodejs/node/pull/35654)
* deps:
* V8: Backport various patches for Apple Silicon support (BoHong Li) (https://github.com/nodejs/node/pull/38051)
* update ICU to 68.1 (Michaël Zasso) (https://github.com/nodejs/node/pull/36187)
* upgrade to libuv 1.41.0 (Colin Ihrig) (https://github.com/nodejs/node/pull/37360)
* http:
* add http.ClientRequest.getRawHeaderNames() (simov) (https://github.com/nodejs/node/pull/37660)
* report request start and end with diagnostics\_channel (Stephen Belanger) (https://github.com/nodejs/node/pull/34895)
* util:
* add getSystemErrorMap() impl (eladkeyshawn) (https://github.com/nodejs/node/pull/38101)
PR-URL: https://github.com/nodejs/node/pull/38507
2021-05-02 23:12:18 -04:00
|
|
|
|
added:
|
|
|
|
|
- v15.13.0
|
|
|
|
|
- v14.17.0
|
2021-03-08 21:27:49 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
|
* Returns: {string\[]}
|
2021-03-08 21:27:49 +02:00
|
|
|
|
|
|
|
|
|
Returns an array containing the unique names of the current outgoing raw
|
|
|
|
|
headers. Header names are returned with their exact casing being set.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
request.setHeader('Foo', 'bar');
|
|
|
|
|
request.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
|
|
|
|
|
const headerNames = request.getRawHeaderNames();
|
|
|
|
|
// headerNames === ['Foo', 'Set-Cookie']
|
|
|
|
|
```
|
|
|
|
|
|
2022-04-18 20:49:05 +02:00
|
|
|
|
### `request.hasHeader(name)`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v7.7.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
|
|
Returns `true` if the header identified by `name` is currently set in the
|
|
|
|
|
outgoing headers. The header name matching is case-insensitive.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const hasContentType = request.hasHeader('content-type');
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.maxHeadersCount`
|
2018-04-23 09:07:00 +09:00
|
|
|
|
|
|
|
|
|
* {number} **Default:** `2000`
|
|
|
|
|
|
|
|
|
|
Limits maximum response headers count. If set to 0, no limit will be applied.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.path`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-01-29 19:40:45 +09:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
|
|
|
|
|
2019-02-22 19:27:41 +09:00
|
|
|
|
* {string} The request path.
|
2019-01-29 19:40:45 +09:00
|
|
|
|
|
2020-06-09 13:42:07 +08:00
|
|
|
|
### `request.method`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-06-09 13:42:07 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.97
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {string} The request method.
|
|
|
|
|
|
|
|
|
|
### `request.host`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-06-09 13:42:07 +08:00
|
|
|
|
<!-- YAML
|
2020-09-28 10:54:13 -07:00
|
|
|
|
added:
|
|
|
|
|
- v14.5.0
|
|
|
|
|
- v12.19.0
|
2020-06-09 13:42:07 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {string} The request host.
|
|
|
|
|
|
|
|
|
|
### `request.protocol`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-06-09 13:42:07 +08:00
|
|
|
|
<!-- YAML
|
2020-09-28 10:54:13 -07:00
|
|
|
|
added:
|
|
|
|
|
- v14.5.0
|
|
|
|
|
- v12.19.0
|
2020-06-09 13:42:07 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {string} The request protocol.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.removeHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-09-01 15:28:12 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.6.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
|
|
|
|
|
|
|
|
|
Removes a header that's already defined into headers object.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
request.removeHeader('Content-Type');
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.reusedSocket`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-09-26 11:56:59 +08:00
|
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.0.0
|
|
|
|
|
- v12.16.0
|
2019-09-26 11:56:59 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean} Whether the request is send through a reused socket.
|
|
|
|
|
|
|
|
|
|
When sending request through a keep-alive enabled agent, the underlying socket
|
|
|
|
|
might be reused. But if server closes connection at unfortunate time, client
|
|
|
|
|
may run into a 'ECONNRESET' error.
|
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
|
|
|
|
|
// Server has a 5 seconds keep-alive timeout by default
|
|
|
|
|
http
|
|
|
|
|
.createServer((req, res) => {
|
|
|
|
|
res.write('hello\n');
|
|
|
|
|
res.end();
|
|
|
|
|
})
|
|
|
|
|
.listen(3000);
|
|
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
// Adapting a keep-alive agent
|
|
|
|
|
http.get('http://localhost:3000', { agent }, (res) => {
|
|
|
|
|
res.on('data', (data) => {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2019-09-26 11:56:59 +08:00
|
|
|
|
|
|
|
|
|
// Server has a 5 seconds keep-alive timeout by default
|
|
|
|
|
http
|
|
|
|
|
.createServer((req, res) => {
|
|
|
|
|
res.write('hello\n');
|
|
|
|
|
res.end();
|
|
|
|
|
})
|
|
|
|
|
.listen(3000);
|
|
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
// Adapting a keep-alive agent
|
|
|
|
|
http.get('http://localhost:3000', { agent }, (res) => {
|
|
|
|
|
res.on('data', (data) => {
|
|
|
|
|
// Do nothing
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, 5000); // Sending request on 5s interval so it's easy to hit idle timeout
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
By marking a request whether it reused socket or not, we can do
|
|
|
|
|
automatic error retry base on it.
|
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
const agent = new http.Agent({ keepAlive: true });
|
|
|
|
|
|
|
|
|
|
function retriableRequest() {
|
|
|
|
|
const req = http
|
|
|
|
|
.get('http://localhost:3000', { agent }, (res) => {
|
|
|
|
|
// ...
|
|
|
|
|
})
|
|
|
|
|
.on('error', (err) => {
|
|
|
|
|
// Check if retry is needed
|
|
|
|
|
if (req.reusedSocket && err.code === 'ECONNRESET') {
|
|
|
|
|
retriableRequest();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retriableRequest();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2019-09-26 11:56:59 +08:00
|
|
|
|
const agent = new http.Agent({ keepAlive: true });
|
|
|
|
|
|
|
|
|
|
function retriableRequest() {
|
|
|
|
|
const req = http
|
|
|
|
|
.get('http://localhost:3000', { agent }, (res) => {
|
|
|
|
|
// ...
|
|
|
|
|
})
|
|
|
|
|
.on('error', (err) => {
|
|
|
|
|
// Check if retry is needed
|
|
|
|
|
if (req.reusedSocket && err.code === 'ECONNRESET') {
|
|
|
|
|
retriableRequest();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retriableRequest();
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.setHeader(name, value)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-09-01 15:28:12 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.6.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
2018-04-09 22:43:37 +02:00
|
|
|
|
* `value` {any}
|
2017-09-01 15:28:12 +02:00
|
|
|
|
|
|
|
|
|
Sets a single header value for headers object. If this header already exists in
|
|
|
|
|
the to-be-sent headers, its value will be replaced. Use an array of strings
|
2018-04-09 22:43:37 +02:00
|
|
|
|
here to send multiple headers with the same name. Non-string values will be
|
|
|
|
|
stored without modification. Therefore, [`request.getHeader()`][] may return
|
|
|
|
|
non-string values. However, the non-string values will be converted to strings
|
|
|
|
|
for network transmission.
|
2017-09-01 15:28:12 +02:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
request.setHeader('Content-Type', 'application/json');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
```js
|
2018-10-17 09:40:50 +02:00
|
|
|
|
request.setHeader('Cookie', ['type=ninja', 'language=javascript']);
|
2017-09-01 15:28:12 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-04-06 11:41:00 +02:00
|
|
|
|
When the value is a string an exception will be thrown if it contains
|
|
|
|
|
characters outside the `latin1` encoding.
|
|
|
|
|
|
|
|
|
|
If you need to pass UTF-8 characters in the value please encode the value
|
|
|
|
|
using the [RFC 8187][] standard.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const filename = 'Rock 🎵.txt';
|
|
|
|
|
request.setHeader('Content-Disposition', `attachment; filename*=utf-8''${encodeURIComponent(filename)}`);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.setNoDelay([noDelay])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
|
|
|
|
-->
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `noDelay` {boolean}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Once a socket is assigned to this request and is connected
|
2015-11-27 18:30:32 -05:00
|
|
|
|
[`socket.setNoDelay()`][] will be called.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.setSocketKeepAlive([enable][, initialDelay])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
|
|
|
|
-->
|
2011-02-10 02:18:13 -08:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `enable` {boolean}
|
|
|
|
|
* `initialDelay` {number}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Once a socket is assigned to this request and is connected
|
2015-11-27 18:30:32 -05:00
|
|
|
|
[`socket.setKeepAlive()`][] will be called.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.setTimeout(timeout[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
2018-12-19 14:59:02 +11:00
|
|
|
|
changes:
|
|
|
|
|
- version: v9.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/8895
|
|
|
|
|
description: Consistently set socket timeout only when the socket connects.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-01-10 20:07:01 -08:00
|
|
|
|
* `timeout` {number} Milliseconds before a request times out.
|
2018-02-12 02:31:55 -05:00
|
|
|
|
* `callback` {Function} Optional function to be called when a timeout occurs.
|
2018-04-09 19:30:22 +03:00
|
|
|
|
Same as binding to the `'timeout'` event.
|
2018-04-11 21:07:14 +03:00
|
|
|
|
* Returns: {http.ClientRequest}
|
2015-12-29 00:10:21 +01:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
Once a socket is assigned to this request and is connected
|
|
|
|
|
[`socket.setTimeout()`][] will be called.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.socket`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-06-11 11:18:03 -07:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* {stream.Duplex}
|
2017-06-11 11:18:03 -07:00
|
|
|
|
|
|
|
|
|
Reference to the underlying socket. Usually users will not want to access
|
|
|
|
|
this property. In particular, the socket will not emit `'readable'` events
|
2020-10-01 10:43:13 +02:00
|
|
|
|
because of how the protocol parser attaches to the socket.
|
2017-06-11 11:18:03 -07:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
const options = {
|
|
|
|
|
host: 'www.google.com',
|
|
|
|
|
};
|
|
|
|
|
const req = http.get(options);
|
|
|
|
|
req.end();
|
|
|
|
|
req.once('response', (res) => {
|
|
|
|
|
const ip = req.socket.localAddress;
|
|
|
|
|
const port = req.socket.localPort;
|
|
|
|
|
console.log(`Your IP address is ${ip} and your source port is ${port}.`);
|
|
|
|
|
// Consume response object
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2017-08-04 17:56:03 +08:00
|
|
|
|
const options = {
|
|
|
|
|
host: 'www.google.com',
|
|
|
|
|
};
|
|
|
|
|
const req = http.get(options);
|
|
|
|
|
req.end();
|
|
|
|
|
req.once('response', (res) => {
|
|
|
|
|
const ip = req.socket.localAddress;
|
|
|
|
|
const port = req.socket.localPort;
|
|
|
|
|
console.log(`Your IP address is ${ip} and your source port is ${port}.`);
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// Consume response object
|
2017-08-04 17:56:03 +08:00
|
|
|
|
});
|
2017-06-11 11:18:03 -07:00
|
|
|
|
```
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This property is guaranteed to be an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specified a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2022-04-18 20:49:05 +02:00
|
|
|
|
### `request.uncork()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
See [`writable.uncork()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.writableEnded`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-08-02 08:09:06 +02:00
|
|
|
|
<!-- YAML
|
2019-08-19 21:14:22 +02:00
|
|
|
|
added: v12.9.0
|
2019-08-02 08:09:06 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
Is `true` after [`request.end()`][] has been called. This property
|
|
|
|
|
does not indicate whether the data has been flushed, for this use
|
|
|
|
|
[`request.writableFinished`][] instead.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.writableFinished`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-08-02 08:09:06 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v12.7.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
Is `true` if all data has been flushed to the underlying system, immediately
|
|
|
|
|
before the [`'finish'`][] event is emitted.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `request.write(chunk[, encoding][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.29
|
2022-10-28 01:45:05 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v15.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33155
|
|
|
|
|
description: The `chunk` parameter can now be a `Uint8Array`.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2022-10-28 01:45:05 +02:00
|
|
|
|
* `chunk` {string|Buffer|Uint8Array}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `encoding` {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
2018-04-11 21:07:14 +03:00
|
|
|
|
* Returns: {boolean}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2019-06-22 11:12:48 +08:00
|
|
|
|
Sends a chunk of the body. This method can be called multiple times. If no
|
|
|
|
|
`Content-Length` is set, data will automatically be encoded in HTTP Chunked
|
|
|
|
|
transfer encoding, so that server knows when the data ends. The
|
|
|
|
|
`Transfer-Encoding: chunked` header is added. Calling [`request.end()`][]
|
|
|
|
|
is necessary to finish sending the request.
|
2012-07-31 13:15:46 +09:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
The `encoding` argument is optional and only applies when `chunk` is a string.
|
|
|
|
|
Defaults to `'utf8'`.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
The `callback` argument is optional and will be called when this chunk of data
|
2018-08-22 14:01:23 -04:00
|
|
|
|
is flushed, but only if the chunk is non-empty.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-02-02 18:40:01 +08:00
|
|
|
|
Returns `true` if the entire data was flushed successfully to the kernel
|
|
|
|
|
buffer. Returns `false` if all or part of the data was queued in user memory.
|
|
|
|
|
`'drain'` will be emitted when the buffer is free again.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-08-21 19:50:04 +05:30
|
|
|
|
When `write` function is called with empty string or buffer, it does
|
|
|
|
|
nothing and waits for more input.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## Class: `http.Server`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.17
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-08-21 13:11:16 -07:00
|
|
|
|
* Extends: {net.Server}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'checkContinue'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `request` {http.IncomingMessage}
|
|
|
|
|
* `response` {http.ServerResponse}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
Emitted each time a request with an HTTP `Expect: 100-continue` is received.
|
2017-01-24 11:37:46 -08:00
|
|
|
|
If this event is not listened for, the server will automatically respond
|
2016-09-10 16:03:30 -07:00
|
|
|
|
with a `100 Continue` as appropriate.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
Handling this event involves calling [`response.writeContinue()`][] if the
|
|
|
|
|
client should continue to send the request body, or generating an appropriate
|
|
|
|
|
HTTP response (e.g. 400 Bad Request) if the client should not continue to send
|
|
|
|
|
the request body.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
When this event is emitted and handled, the [`'request'`][] event will
|
2016-09-10 16:03:30 -07:00
|
|
|
|
not be emitted.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'checkExpectation'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v5.5.0
|
|
|
|
|
-->
|
|
|
|
|
|
2017-08-04 17:56:03 +08:00
|
|
|
|
* `request` {http.IncomingMessage}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `response` {http.ServerResponse}
|
|
|
|
|
|
|
|
|
|
Emitted each time a request with an HTTP `Expect` header is received, where the
|
2017-01-24 11:37:46 -08:00
|
|
|
|
value is not `100-continue`. If this event is not listened for, the server will
|
2016-09-10 16:03:30 -07:00
|
|
|
|
automatically respond with a `417 Expectation Failed` as appropriate.
|
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
When this event is emitted and handled, the [`'request'`][] event will
|
2015-11-04 17:56:03 -05:00
|
|
|
|
not be emitted.
|
2013-12-10 18:56:04 +08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'clientError'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.94
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-10-01 20:49:03 +02:00
|
|
|
|
- version: v12.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/25605
|
|
|
|
|
description: The default behavior will return a 431 Request Header
|
|
|
|
|
Fields Too Large if a HPE_HEADER_OVERFLOW error occurs.
|
2018-01-09 19:23:55 -05:00
|
|
|
|
- version: v9.4.0
|
2017-12-14 12:40:32 +08:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/17672
|
2018-04-29 20:46:41 +03:00
|
|
|
|
description: The `rawPacket` is the current buffer that just parsed. Adding
|
2018-04-09 19:30:22 +03:00
|
|
|
|
this buffer to the error object of `'clientError'` event is to
|
|
|
|
|
make it possible that developers can log the broken packet.
|
2020-10-01 20:49:03 +02:00
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/4557
|
|
|
|
|
description: The default action of calling `.destroy()` on the `socket`
|
|
|
|
|
will no longer take place if there are listeners attached
|
|
|
|
|
for `'clientError'`.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2013-12-10 18:56:04 +08:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `exception` {Error}
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
If a client connection emits an `'error'` event, it will be forwarded here.
|
2016-01-06 17:00:27 -05:00
|
|
|
|
Listener of this event is responsible for closing/destroying the underlying
|
2018-02-20 18:33:05 +01:00
|
|
|
|
socket. For example, one may wish to more gracefully close the socket with a
|
2023-02-12 17:42:03 +01:00
|
|
|
|
custom HTTP response instead of abruptly severing the connection. The socket
|
|
|
|
|
**must be closed or destroyed** before the listener ends.
|
2016-01-06 17:00:27 -05:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2019-01-21 17:47:32 +11:00
|
|
|
|
Default behavior is to try close the socket with a HTTP '400 Bad Request',
|
|
|
|
|
or a HTTP '431 Request Header Fields Too Large' in the case of a
|
2022-10-07 01:51:47 +08:00
|
|
|
|
[`HPE_HEADER_OVERFLOW`][] error. If the socket is not writable or headers
|
|
|
|
|
of the current attached [`http.ServerResponse`][] has been sent, it is
|
|
|
|
|
immediately destroyed.
|
2012-05-16 16:27:34 +02:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
`socket` is the [`net.Socket`][] object that the error originated from.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
|
|
|
|
server.on('clientError', (err, socket) => {
|
|
|
|
|
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
|
|
|
});
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2016-02-15 14:48:00 -08:00
|
|
|
|
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
|
|
|
|
server.on('clientError', (err, socket) => {
|
|
|
|
|
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
|
|
|
});
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
When the `'clientError'` event occurs, there is no `request` or `response`
|
|
|
|
|
object, so any HTTP response sent, including response headers and payload,
|
2021-10-10 21:55:04 -07:00
|
|
|
|
_must_ be written directly to the `socket` object. Care must be taken to
|
2016-02-15 14:48:00 -08:00
|
|
|
|
ensure the response is a properly formatted HTTP response message.
|
|
|
|
|
|
2017-12-14 12:40:32 +08:00
|
|
|
|
`err` is an instance of `Error` with two extra columns:
|
|
|
|
|
|
2019-09-13 00:22:29 -04:00
|
|
|
|
* `bytesParsed`: the bytes count of request packet that Node.js may have parsed
|
2017-12-14 12:40:32 +08:00
|
|
|
|
correctly;
|
2019-09-13 00:22:29 -04:00
|
|
|
|
* `rawPacket`: the raw packet of current request.
|
2017-12-14 12:40:32 +08:00
|
|
|
|
|
2020-05-08 12:27:52 +02:00
|
|
|
|
In some cases, the client has already received the response and/or the socket
|
|
|
|
|
has already been destroyed, like in case of `ECONNRESET` errors. Before
|
|
|
|
|
trying to send data to the socket, it is better to check that it is still
|
|
|
|
|
writable.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
server.on('clientError', (err, socket) => {
|
|
|
|
|
if (err.code === 'ECONNRESET' || !socket.writable) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'close'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.4
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Emitted when the server closes.
|
2013-11-28 22:25:30 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'connect'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
|
|
|
|
|
the [`'request'`][] event
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex} Network socket between the server and client
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `head` {Buffer} The first packet of the tunneling stream (may be empty)
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2017-01-24 11:37:46 -08:00
|
|
|
|
Emitted each time a client requests an HTTP `CONNECT` method. If this event is
|
|
|
|
|
not listened for, then clients requesting a `CONNECT` method will have their
|
2015-11-04 17:56:03 -05:00
|
|
|
|
connections closed.
|
2014-09-12 06:25:15 -03:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
After this event is emitted, the request's socket will not have a `'data'`
|
2017-03-03 15:45:20 -08:00
|
|
|
|
event listener, meaning it will need to be bound in order to handle data
|
2015-11-04 17:56:03 -05:00
|
|
|
|
sent to the server on that socket.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'connection'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.0
|
|
|
|
|
-->
|
2011-05-05 15:40:45 -07:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex}
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2017-10-17 22:19:27 +02:00
|
|
|
|
This event is emitted when a new TCP stream is established. `socket` is
|
|
|
|
|
typically an object of type [`net.Socket`][]. Usually users will not want to
|
|
|
|
|
access this event. In particular, the socket will not emit `'readable'` events
|
|
|
|
|
because of how the protocol parser attaches to the socket. The `socket` can
|
2020-10-01 10:43:13 +02:00
|
|
|
|
also be accessed at `request.socket`.
|
2017-10-17 22:19:27 +02:00
|
|
|
|
|
2018-02-05 21:55:16 -08:00
|
|
|
|
This event can also be explicitly emitted by users to inject connections
|
2017-10-17 22:19:27 +02:00
|
|
|
|
into the HTTP server. In that case, any [`Duplex`][] stream can be passed.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-01-27 23:09:17 +00:00
|
|
|
|
If `socket.setTimeout()` is called here, the timeout will be replaced with
|
|
|
|
|
`server.keepAliveTimeout` when the socket has served a request (if
|
|
|
|
|
`server.keepAliveTimeout` is non-zero).
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2022-07-23 00:33:03 +08:00
|
|
|
|
### Event: `'dropRequest'`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
|
added:
|
|
|
|
|
- v18.7.0
|
|
|
|
|
- v16.17.0
|
2022-07-23 00:33:03 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
|
|
|
|
|
the [`'request'`][] event
|
|
|
|
|
* `socket` {stream.Duplex} Network socket between the server and client
|
|
|
|
|
|
|
|
|
|
When the number of requests on a socket reaches the threshold of
|
|
|
|
|
`server.maxRequestsPerSocket`, the server will drop new requests
|
|
|
|
|
and emit `'dropRequest'` event instead, then send `503` to client.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'request'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.0
|
|
|
|
|
-->
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `request` {http.IncomingMessage}
|
|
|
|
|
* `response` {http.ServerResponse}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
Emitted each time there is a request. There may be multiple requests
|
2017-01-09 12:45:46 -05:00
|
|
|
|
per connection (in the case of HTTP Keep-Alive connections).
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'upgrade'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.94
|
2018-04-12 19:57:19 +02:00
|
|
|
|
changes:
|
2018-03-02 09:53:46 -08:00
|
|
|
|
- version: v10.0.0
|
2020-10-09 21:08:06 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/19981
|
2018-04-12 19:57:19 +02:00
|
|
|
|
description: Not listening to this event no longer causes the socket
|
|
|
|
|
to be destroyed if a client sends an Upgrade header.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
|
|
|
|
|
the [`'request'`][] event
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* `socket` {stream.Duplex} Network socket between the server and client
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `head` {Buffer} The first packet of the upgraded stream (may be empty)
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-04-12 19:57:19 +02:00
|
|
|
|
Emitted each time a client requests an HTTP upgrade. Listening to this event
|
|
|
|
|
is optional and clients cannot insist on a protocol change.
|
2011-10-19 15:06:49 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
After this event is emitted, the request's socket will not have a `'data'`
|
2017-03-03 15:45:20 -08:00
|
|
|
|
event listener, meaning it will need to be bound in order to handle data
|
2015-11-04 17:56:03 -05:00
|
|
|
|
sent to the server on that socket.
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This event is guaranteed to be passed an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specifies a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.close([callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
2022-06-21 14:50:55 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version:
|
2022-09-13 12:53:52 -03:00
|
|
|
|
- v19.0.0
|
2022-06-21 14:50:55 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/43522
|
|
|
|
|
description: The method closes idle connections before returning.
|
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
|
|
|
|
|
2022-06-21 14:50:55 +02:00
|
|
|
|
Stops the server from accepting new connections and closes all connections
|
|
|
|
|
connected to this server which are not sending a request or waiting for
|
|
|
|
|
a response.
|
|
|
|
|
See [`net.Server.close()`][].
|
2011-01-20 17:54:59 -08:00
|
|
|
|
|
2024-05-11 11:41:51 -07:00
|
|
|
|
```js
|
|
|
|
|
const http = require('node:http');
|
|
|
|
|
|
|
|
|
|
const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => {
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
|
data: 'Hello World!',
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
// Close the server after 10 seconds
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
server.close(() => {
|
|
|
|
|
console.log('server on port 8000 closed successfully');
|
|
|
|
|
});
|
|
|
|
|
}, 10000);
|
|
|
|
|
```
|
|
|
|
|
|
2022-04-28 12:05:55 +02:00
|
|
|
|
### `server.closeAllConnections()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-05-10 10:10:45 -03:00
|
|
|
|
added: v18.2.0
|
2022-04-28 12:05:55 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2024-06-25 11:32:47 +01:00
|
|
|
|
Closes all established HTTP(S) connections connected to this server, including
|
|
|
|
|
active connections connected to this server which are sending a request or
|
|
|
|
|
waiting for a response. This does _not_ destroy sockets upgraded to a different
|
|
|
|
|
protocol, such as WebSocket or HTTP/2.
|
2024-05-11 11:41:51 -07:00
|
|
|
|
|
|
|
|
|
> This is a forceful way of closing all connections and should be used with
|
|
|
|
|
> caution. Whenever using this in conjunction with `server.close`, calling this
|
|
|
|
|
> _after_ `server.close` is recommended as to avoid race conditions where new
|
|
|
|
|
> connections are created between a call to this and a call to `server.close`.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const http = require('node:http');
|
|
|
|
|
|
|
|
|
|
const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => {
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
|
data: 'Hello World!',
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
// Close the server after 10 seconds
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
server.close(() => {
|
|
|
|
|
console.log('server on port 8000 closed successfully');
|
|
|
|
|
});
|
|
|
|
|
// Closes all connections, ensuring the server closes successfully
|
|
|
|
|
server.closeAllConnections();
|
|
|
|
|
}, 10000);
|
|
|
|
|
```
|
2022-04-28 12:05:55 +02:00
|
|
|
|
|
|
|
|
|
### `server.closeIdleConnections()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-05-10 10:10:45 -03:00
|
|
|
|
added: v18.2.0
|
2022-04-28 12:05:55 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Closes all connections connected to this server which are not sending a request
|
|
|
|
|
or waiting for a response.
|
|
|
|
|
|
2024-05-11 11:41:51 -07:00
|
|
|
|
> Starting with Node.js 19.0.0, there's no need for calling this method in
|
|
|
|
|
> conjunction with `server.close` to reap `keep-alive` connections. Using it
|
|
|
|
|
> won't cause any harm though, and it can be useful to ensure backwards
|
|
|
|
|
> compatibility for libraries and applications that need to support versions
|
|
|
|
|
> older than 19.0.0. Whenever using this in conjunction with `server.close`,
|
|
|
|
|
> calling this _after_ `server.close` is recommended as to avoid race
|
|
|
|
|
> conditions where new connections are created between a call to this and a
|
|
|
|
|
> call to `server.close`.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const http = require('node:http');
|
|
|
|
|
|
|
|
|
|
const server = http.createServer({ keepAliveTimeout: 60000 }, (req, res) => {
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
|
|
|
res.end(JSON.stringify({
|
|
|
|
|
data: 'Hello World!',
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
// Close the server after 10 seconds
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
server.close(() => {
|
|
|
|
|
console.log('server on port 8000 closed successfully');
|
|
|
|
|
});
|
|
|
|
|
// Closes idle connections, such as keep-alive connections. Server will close
|
|
|
|
|
// once remaining active connections are terminated
|
|
|
|
|
server.closeIdleConnections();
|
|
|
|
|
}, 10000);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.headersTimeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-11-28 16:48:07 +02:00
|
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
|
added:
|
|
|
|
|
- v11.3.0
|
|
|
|
|
- v10.14.0
|
2022-12-24 09:28:46 +01:00
|
|
|
|
changes:
|
2023-01-28 16:47:47 -05:00
|
|
|
|
- version:
|
|
|
|
|
- v19.4.0
|
|
|
|
|
- v18.14.0
|
2022-12-24 09:28:46 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/45778
|
|
|
|
|
description: The default is now set to the minimum between 60000 (60 seconds) or `requestTimeout`.
|
2018-11-28 16:48:07 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2022-12-24 09:28:46 +01:00
|
|
|
|
* {number} **Default:** The minimum between [`server.requestTimeout`][] or `60000`.
|
2018-11-28 16:48:07 +02:00
|
|
|
|
|
|
|
|
|
Limit the amount of time the parser will wait to receive the complete HTTP
|
|
|
|
|
headers.
|
|
|
|
|
|
2022-04-13 16:47:59 +02:00
|
|
|
|
If the timeout expires, the server responds with status 408 without
|
|
|
|
|
forwarding the request to the request listener and then closes the connection.
|
|
|
|
|
|
|
|
|
|
It must be set to a non-zero value (e.g. 120 seconds) to protect against
|
|
|
|
|
potential Denial-of-Service attacks in case the server is deployed without a
|
|
|
|
|
reverse proxy in front.
|
2018-11-28 16:48:07 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.listen()`
|
2016-08-26 21:26:28 -07:00
|
|
|
|
|
2017-10-06 11:50:47 -07:00
|
|
|
|
Starts the HTTP server listening for connections.
|
|
|
|
|
This method is identical to [`server.listen()`][] from [`net.Server`][].
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.listening`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v5.7.0
|
|
|
|
|
-->
|
2016-01-18 13:36:48 +00:00
|
|
|
|
|
2018-04-29 20:46:41 +03:00
|
|
|
|
* {boolean} Indicates whether or not the server is listening for connections.
|
2016-01-18 13:36:48 +00:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.maxHeadersCount`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2018-04-02 04:44:32 +03:00
|
|
|
|
* {number} **Default:** `2000`
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2018-04-23 09:07:00 +09:00
|
|
|
|
Limits maximum incoming headers count. If set to 0, no limit will be applied.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2020-05-14 20:21:34 +02:00
|
|
|
|
### `server.requestTimeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-05-14 20:21:34 +02:00
|
|
|
|
<!-- YAML
|
2020-09-14 16:02:57 -04:00
|
|
|
|
added: v14.11.0
|
2022-04-13 16:47:59 +02:00
|
|
|
|
changes:
|
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
|
|
|
|
- version: v18.0.0
|
2022-04-13 16:47:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/41263
|
|
|
|
|
description: The default request timeout changed
|
|
|
|
|
from no timeout to 300s (5 minutes).
|
2020-05-14 20:21:34 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2022-04-13 16:47:59 +02:00
|
|
|
|
* {number} **Default:** `300000`
|
2020-05-14 20:21:34 +02:00
|
|
|
|
|
|
|
|
|
Sets the timeout value in milliseconds for receiving the entire request from
|
|
|
|
|
the client.
|
|
|
|
|
|
|
|
|
|
If the timeout expires, the server responds with status 408 without
|
|
|
|
|
forwarding the request to the request listener and then closes the connection.
|
|
|
|
|
|
2021-01-18 15:17:56 +02:00
|
|
|
|
It must be set to a non-zero value (e.g. 120 seconds) to protect against
|
2020-05-14 20:21:34 +02:00
|
|
|
|
potential Denial-of-Service attacks in case the server is deployed without a
|
|
|
|
|
reverse proxy in front.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.setTimeout([msecs][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.12
|
2019-05-03 16:45:57 -07:00
|
|
|
|
changes:
|
2019-09-09 13:32:18 +01:00
|
|
|
|
- version: v13.0.0
|
2019-05-03 16:45:57 -07:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/27558
|
|
|
|
|
description: The default timeout changed from 120s to 0 (no timeout).
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2019-05-03 16:45:57 -07:00
|
|
|
|
* `msecs` {number} **Default:** 0 (no timeout)
|
2015-11-04 17:56:03 -05:00
|
|
|
|
* `callback` {Function}
|
2018-04-11 21:07:14 +03:00
|
|
|
|
* Returns: {http.Server}
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Sets the timeout value for sockets, and emits a `'timeout'` event on
|
|
|
|
|
the Server object, passing the socket as an argument, if a timeout
|
|
|
|
|
occurs.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
If there is a `'timeout'` event listener on the Server object, then it
|
|
|
|
|
will be called with the timed-out socket as an argument.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2019-05-03 16:45:57 -07:00
|
|
|
|
By default, the Server does not timeout sockets. However, if a callback
|
|
|
|
|
is assigned to the Server's `'timeout'` event, timeouts must be handled
|
|
|
|
|
explicitly.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2021-09-11 14:55:09 +03:00
|
|
|
|
### `server.maxRequestsPerSocket`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-09-11 14:55:09 +03:00
|
|
|
|
<!-- YAML
|
2021-09-22 00:43:16 +01:00
|
|
|
|
added: v16.10.0
|
2021-09-11 14:55:09 +03:00
|
|
|
|
-->
|
|
|
|
|
|
2021-09-23 20:13:53 +03:00
|
|
|
|
* {number} Requests per socket. **Default:** 0 (no limit)
|
2021-09-11 14:55:09 +03:00
|
|
|
|
|
|
|
|
|
The maximum number of requests socket can handle
|
|
|
|
|
before closing keep alive connection.
|
|
|
|
|
|
2021-09-23 20:13:53 +03:00
|
|
|
|
A value of `0` will disable the limit.
|
2021-09-11 14:55:09 +03:00
|
|
|
|
|
2021-09-20 11:30:03 +02:00
|
|
|
|
When the limit is reached it will set the `Connection` header value to `close`,
|
2021-09-11 14:55:09 +03:00
|
|
|
|
but will not actually close the connection, subsequent requests sent
|
|
|
|
|
after the limit is reached will get `503 Service Unavailable` as a response.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.timeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.12
|
2019-05-03 16:45:57 -07:00
|
|
|
|
changes:
|
2019-09-09 13:32:18 +01:00
|
|
|
|
- version: v13.0.0
|
2019-05-03 16:45:57 -07:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/27558
|
|
|
|
|
description: The default timeout changed from 120s to 0 (no timeout).
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2019-05-03 16:45:57 -07:00
|
|
|
|
* {number} Timeout in milliseconds. **Default:** 0 (no timeout)
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
The number of milliseconds of inactivity before a socket is presumed
|
|
|
|
|
to have timed out.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2017-12-13 10:47:30 -08:00
|
|
|
|
A value of `0` will disable the timeout behavior on incoming connections.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2018-02-05 21:55:16 -08:00
|
|
|
|
The socket timeout logic is set up on connection, so changing this
|
2015-10-29 21:53:43 +02:00
|
|
|
|
value only affects new connections to the server, not any existing connections.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `server.keepAliveTimeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2015-10-29 21:53:43 +02:00
|
|
|
|
<!-- YAML
|
2017-03-15 20:26:14 -07:00
|
|
|
|
added: v8.0.0
|
2015-10-29 21:53:43 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2018-04-02 04:44:32 +03:00
|
|
|
|
* {number} Timeout in milliseconds. **Default:** `5000` (5 seconds).
|
2015-10-29 21:53:43 +02:00
|
|
|
|
|
|
|
|
|
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. If the server receives new data before the keep-alive
|
|
|
|
|
timeout has fired, it will reset the regular inactivity timeout, i.e.,
|
|
|
|
|
[`server.timeout`][].
|
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
A value of `0` will disable the keep-alive timeout behavior on incoming
|
|
|
|
|
connections.
|
|
|
|
|
A value of `0` makes the http server behave similarly to Node.js versions prior
|
|
|
|
|
to 8.0.0, which did not have a keep-alive timeout.
|
2015-10-29 21:53:43 +02:00
|
|
|
|
|
2018-02-05 21:55:16 -08:00
|
|
|
|
The socket timeout logic is set up on connection, so changing this value only
|
|
|
|
|
affects new connections to the server, not any existing connections.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2023-06-25 20:19:40 +03:00
|
|
|
|
### `server[Symbol.asyncDispose]()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-07-03 10:45:00 -03:00
|
|
|
|
added: v20.4.0
|
2023-06-25 20:19:40 +03:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
> Stability: 1 - Experimental
|
|
|
|
|
|
|
|
|
|
Calls [`server.close()`][] and returns a promise that fulfills when the
|
|
|
|
|
server has closed.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## Class: `http.ServerResponse`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.17
|
|
|
|
|
-->
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2022-04-14 09:24:19 +02:00
|
|
|
|
* Extends: {http.OutgoingMessage}
|
2019-08-21 13:11:16 -07:00
|
|
|
|
|
2020-03-03 21:23:59 -08:00
|
|
|
|
This object is created internally by an HTTP server, not by the user. It is
|
2016-09-10 16:03:30 -07:00
|
|
|
|
passed as the second parameter to the [`'request'`][] event.
|
2013-08-04 21:30:23 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'close'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.6.7
|
|
|
|
|
-->
|
2012-02-27 11:09:33 -08:00
|
|
|
|
|
2021-03-08 23:53:20 +01:00
|
|
|
|
Indicates that the response is completed, or its underlying connection was
|
2020-07-21 16:28:33 -07:00
|
|
|
|
terminated prematurely (before the response completion).
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'finish'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Emitted when the response has been sent. More specifically, this event is
|
|
|
|
|
emitted when the last segment of the response headers and body have been
|
|
|
|
|
handed off to the operating system for transmission over the network. It
|
|
|
|
|
does not imply that the client has received anything yet.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.addTrailers(headers)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `headers` {Object}
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
This method adds HTTP trailing headers (a header but at the end of the
|
|
|
|
|
message) to the response.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Trailers will **only** be emitted if chunked encoding is used for the
|
2016-11-12 16:19:47 +09:00
|
|
|
|
response; if it is not (e.g. if the request was HTTP/1.0), they will
|
2015-11-04 17:56:03 -05:00
|
|
|
|
be silently discarded.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
HTTP requires the `Trailer` header to be sent in order to
|
2015-11-04 17:56:03 -05:00
|
|
|
|
emit trailers, with a list of the header fields in its value. E.g.,
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
response.writeHead(200, { 'Content-Type': 'text/plain',
|
|
|
|
|
'Trailer': 'Content-MD5' });
|
|
|
|
|
response.write(fileData);
|
2017-06-01 02:07:25 +03:00
|
|
|
|
response.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' });
|
2016-01-17 18:39:07 +01:00
|
|
|
|
response.end();
|
|
|
|
|
```
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2016-02-03 17:32:23 -08:00
|
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
|
|
|
|
will result in a [`TypeError`][] being thrown.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.connection`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-06-11 11:18:03 -07:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
2019-09-09 13:32:18 +01:00
|
|
|
|
deprecated: v13.0.0
|
2017-06-11 11:18:03 -07:00
|
|
|
|
-->
|
|
|
|
|
|
2019-08-06 13:56:52 +02:00
|
|
|
|
> Stability: 0 - Deprecated. Use [`response.socket`][].
|
2019-10-01 18:57:09 +03:00
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* {stream.Duplex}
|
2019-08-06 13:56:52 +02:00
|
|
|
|
|
2017-06-11 11:18:03 -07:00
|
|
|
|
See [`response.socket`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.cork()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-08-08 21:12:41 +02:00
|
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
2019-08-08 21:12:41 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
See [`writable.cork()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.end([data[, encoding]][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
2018-02-14 12:32:01 +00:00
|
|
|
|
changes:
|
2022-10-28 01:45:05 +02:00
|
|
|
|
- version: v15.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33155
|
|
|
|
|
description: The `data` parameter can now be a `Uint8Array`.
|
2018-03-02 09:53:46 -08:00
|
|
|
|
- version: v10.0.0
|
2018-02-14 12:32:01 +00:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/18780
|
|
|
|
|
description: This method now returns a reference to `ServerResponse`.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-04-25 16:04:07 -07:00
|
|
|
|
|
2022-10-28 01:45:05 +02:00
|
|
|
|
* `data` {string|Buffer|Uint8Array}
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `encoding` {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
2018-02-14 12:32:01 +00:00
|
|
|
|
* Returns: {this}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
This method signals to the server that all of the response headers and body
|
|
|
|
|
have been sent; that server should consider this message complete.
|
2015-11-27 18:30:32 -05:00
|
|
|
|
The method, `response.end()`, MUST be called on each response.
|
2011-04-25 16:04:07 -07:00
|
|
|
|
|
2019-03-06 10:26:33 +01:00
|
|
|
|
If `data` is specified, it is similar in effect to calling
|
2015-11-27 18:30:32 -05:00
|
|
|
|
[`response.write(data, encoding)`][] followed by `response.end(callback)`.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
If `callback` is specified, it will be called when the response stream
|
|
|
|
|
is finished.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.finished`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.0.2
|
2020-04-24 18:43:06 +02:00
|
|
|
|
deprecated:
|
|
|
|
|
- v13.4.0
|
|
|
|
|
- v12.16.0
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
|
2019-07-14 16:59:25 +02:00
|
|
|
|
> Stability: 0 - Deprecated. Use [`response.writableEnded`][].
|
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {boolean}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2019-06-25 00:40:24 +02:00
|
|
|
|
The `response.finished` property will be `true` if [`response.end()`][]
|
|
|
|
|
has been called.
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.flushHeaders()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-07-22 17:30:20 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.6.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Flushes the response headers. See also: [`request.flushHeaders()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.getHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `name` {string}
|
2018-04-09 22:43:37 +02:00
|
|
|
|
* Returns: {any}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2017-01-15 18:16:53 +05:30
|
|
|
|
Reads out a header that's already been queued but not sent to the client.
|
2019-06-20 13:52:54 -06:00
|
|
|
|
The name is case-insensitive. The type of the return value depends
|
2018-04-09 22:43:37 +02:00
|
|
|
|
on the arguments provided to [`response.setHeader()`][].
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2018-04-09 22:43:37 +02:00
|
|
|
|
response.setHeader('Content-Type', 'text/html');
|
|
|
|
|
response.setHeader('Content-Length', Buffer.byteLength(body));
|
|
|
|
|
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const contentType = response.getHeader('content-type');
|
2018-04-09 22:43:37 +02:00
|
|
|
|
// contentType is 'text/html'
|
|
|
|
|
const contentLength = response.getHeader('Content-Length');
|
|
|
|
|
// contentLength is of type number
|
|
|
|
|
const setCookie = response.getHeader('set-cookie');
|
|
|
|
|
// setCookie is of type string[]
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.getHeaderNames()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-01-14 09:39:58 -05:00
|
|
|
|
<!-- YAML
|
2017-02-27 22:49:09 -05:00
|
|
|
|
added: v7.7.0
|
2017-01-14 09:39:58 -05:00
|
|
|
|
-->
|
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
|
* Returns: {string\[]}
|
2017-01-14 09:39:58 -05:00
|
|
|
|
|
|
|
|
|
Returns an array containing the unique names of the current outgoing headers.
|
|
|
|
|
All header names are lowercase.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
response.setHeader('Foo', 'bar');
|
|
|
|
|
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const headerNames = response.getHeaderNames();
|
2017-01-14 09:39:58 -05:00
|
|
|
|
// headerNames === ['foo', 'set-cookie']
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.getHeaders()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-01-14 09:39:58 -05:00
|
|
|
|
<!-- YAML
|
2017-02-27 22:49:09 -05:00
|
|
|
|
added: v7.7.0
|
2017-01-14 09:39:58 -05:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {Object}
|
|
|
|
|
|
|
|
|
|
Returns a shallow copy of the current outgoing headers. Since a shallow copy
|
|
|
|
|
is used, array values may be mutated without additional calls to various
|
|
|
|
|
header-related http module methods. The keys of the returned object are the
|
|
|
|
|
header names and the values are the respective header values. All header names
|
|
|
|
|
are lowercase.
|
|
|
|
|
|
2018-02-05 21:55:16 -08:00
|
|
|
|
The object returned by the `response.getHeaders()` method _does not_
|
2017-05-07 14:50:10 -04:00
|
|
|
|
prototypically inherit from the JavaScript `Object`. This means that typical
|
|
|
|
|
`Object` methods such as `obj.toString()`, `obj.hasOwnProperty()`, and others
|
2021-10-10 21:55:04 -07:00
|
|
|
|
are not defined and _will not work_.
|
2017-05-07 14:50:10 -04:00
|
|
|
|
|
2017-01-14 09:39:58 -05:00
|
|
|
|
```js
|
|
|
|
|
response.setHeader('Foo', 'bar');
|
|
|
|
|
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const headers = response.getHeaders();
|
2017-01-14 09:39:58 -05:00
|
|
|
|
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.hasHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-01-14 09:39:58 -05:00
|
|
|
|
<!-- YAML
|
2017-02-27 22:49:09 -05:00
|
|
|
|
added: v7.7.0
|
2017-01-14 09:39:58 -05:00
|
|
|
|
-->
|
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* `name` {string}
|
|
|
|
|
* Returns: {boolean}
|
2017-01-14 09:39:58 -05:00
|
|
|
|
|
|
|
|
|
Returns `true` if the header identified by `name` is currently set in the
|
2019-06-20 13:52:54 -06:00
|
|
|
|
outgoing headers. The header name matching is case-insensitive.
|
2017-01-14 09:39:58 -05:00
|
|
|
|
|
|
|
|
|
```js
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const hasContentType = response.hasHeader('content-type');
|
2017-01-14 09:39:58 -05:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.headersSent`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.3
|
|
|
|
|
-->
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {boolean}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Boolean (read-only). True if headers were sent, false otherwise.
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.removeHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `name` {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Removes a header that's queued for implicit sending.
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
response.removeHeader('Content-Encoding');
|
|
|
|
|
```
|
2012-01-09 03:51:06 +01:00
|
|
|
|
|
2020-12-13 16:25:14 -08:00
|
|
|
|
### `response.req`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-12-13 16:25:14 -08:00
|
|
|
|
<!-- YAML
|
2021-01-21 21:37:00 -05:00
|
|
|
|
added: v15.7.0
|
2020-12-13 16:25:14 -08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {http.IncomingMessage}
|
|
|
|
|
|
|
|
|
|
A reference to the original HTTP `request` object.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.sendDate`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.5
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {boolean}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
When true, the Date header will be automatically generated and sent in
|
|
|
|
|
the response if it is not already present in the headers. Defaults to true.
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
This should only be disabled for testing; HTTP requires the Date header
|
|
|
|
|
in responses.
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.setHeader(name, value)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `name` {string}
|
2018-04-09 22:43:37 +02:00
|
|
|
|
* `value` {any}
|
2020-11-02 18:52:51 +05:30
|
|
|
|
* Returns: {http.ServerResponse}
|
|
|
|
|
|
|
|
|
|
Returns the response object.
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2018-04-02 08:38:48 +03:00
|
|
|
|
Sets a single header value for implicit headers. If this header already exists
|
|
|
|
|
in the to-be-sent headers, its value will be replaced. Use an array of strings
|
2018-04-09 22:43:37 +02:00
|
|
|
|
here to send multiple headers with the same name. Non-string values will be
|
|
|
|
|
stored without modification. Therefore, [`response.getHeader()`][] may return
|
|
|
|
|
non-string values. However, the non-string values will be converted to strings
|
2020-11-02 18:52:51 +05:30
|
|
|
|
for network transmission. The same response object is returned to the caller,
|
|
|
|
|
to enable call chaining.
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
response.setHeader('Content-Type', 'text/html');
|
|
|
|
|
```
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
or
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
|
|
|
|
|
```
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2016-02-03 17:32:23 -08:00
|
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
|
|
|
|
will result in a [`TypeError`][] being thrown.
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
When headers have been set with [`response.setHeader()`][], they will be merged
|
|
|
|
|
with any headers passed to [`response.writeHead()`][], with the headers passed
|
|
|
|
|
to [`response.writeHead()`][] given precedence.
|
2016-02-05 10:48:41 -03:00
|
|
|
|
|
|
|
|
|
```js
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Returns content-type = text/plain
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const server = http.createServer((req, res) => {
|
2016-02-05 10:48:41 -03:00
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
res.setHeader('X-Foo', 'bar');
|
2017-06-01 02:07:25 +03:00
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-02-05 10:48:41 -03:00
|
|
|
|
res.end('ok');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2018-06-12 10:41:07 -04:00
|
|
|
|
If [`response.writeHead()`][] method is called and this method has not been
|
|
|
|
|
called, it will directly write the supplied header values onto the network
|
|
|
|
|
channel without caching internally, and the [`response.getHeader()`][] on the
|
|
|
|
|
header will not yield the expected result. If progressive population of headers
|
|
|
|
|
is desired with potential future retrieval and modification, use
|
|
|
|
|
[`response.setHeader()`][] instead of [`response.writeHead()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.setTimeout(msecs[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.12
|
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `msecs` {number}
|
2015-11-04 17:56:03 -05:00
|
|
|
|
* `callback` {Function}
|
2018-04-11 21:07:14 +03:00
|
|
|
|
* Returns: {http.ServerResponse}
|
2011-06-11 22:09:40 +09:00
|
|
|
|
|
2018-04-02 08:38:48 +03:00
|
|
|
|
Sets the Socket's timeout value to `msecs`. If a callback is
|
2015-11-04 17:56:03 -05:00
|
|
|
|
provided, then it is added as a listener on the `'timeout'` event on
|
|
|
|
|
the response object.
|
2011-06-11 22:09:40 +09:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
If no `'timeout'` listener is added to the request, the response, or
|
2018-04-02 08:38:48 +03:00
|
|
|
|
the server, then sockets are destroyed when they time out. If a handler is
|
2017-03-03 15:45:20 -08:00
|
|
|
|
assigned to the request, the response, or the server's `'timeout'` events,
|
|
|
|
|
timed out sockets must be handled explicitly.
|
2011-06-11 22:09:40 +09:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.socket`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-06-11 11:18:03 -07:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* {stream.Duplex}
|
2017-06-11 11:18:03 -07:00
|
|
|
|
|
|
|
|
|
Reference to the underlying socket. Usually users will not want to access
|
|
|
|
|
this property. In particular, the socket will not emit `'readable'` events
|
|
|
|
|
because of how the protocol parser attaches to the socket. After
|
2020-10-01 10:43:13 +02:00
|
|
|
|
`response.end()`, the property is nulled.
|
2017-06-11 11:18:03 -07:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
|
const ip = res.socket.remoteAddress;
|
|
|
|
|
const port = res.socket.remotePort;
|
|
|
|
|
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
|
|
|
|
|
}).listen(3000);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2017-06-11 11:18:03 -07:00
|
|
|
|
const server = http.createServer((req, res) => {
|
2017-08-04 17:56:03 +08:00
|
|
|
|
const ip = res.socket.remoteAddress;
|
|
|
|
|
const port = res.socket.remotePort;
|
2017-06-11 11:18:03 -07:00
|
|
|
|
res.end(`Your IP address is ${ip} and your source port is ${port}.`);
|
|
|
|
|
}).listen(3000);
|
|
|
|
|
```
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This property is guaranteed to be an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specified a socket
|
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.statusCode`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
2015-02-24 18:11:11 -06:00
|
|
|
|
|
2019-08-05 13:03:42 -04:00
|
|
|
|
* {number} **Default:** `200`
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
When using implicit headers (not calling [`response.writeHead()`][] explicitly),
|
2015-11-04 17:56:03 -05:00
|
|
|
|
this property controls the status code that will be sent to the client when
|
|
|
|
|
the headers get flushed.
|
2015-02-24 18:11:11 -06:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
response.statusCode = 404;
|
|
|
|
|
```
|
2014-04-09 15:02:03 +02:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
After response header was sent to the client, this property indicates the
|
|
|
|
|
status code which was sent out.
|
2014-04-09 15:02:03 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.statusMessage`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.8
|
|
|
|
|
-->
|
2014-04-09 15:02:03 +02:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
When using implicit headers (not calling [`response.writeHead()`][] explicitly),
|
|
|
|
|
this property controls the status message that will be sent to the client when
|
|
|
|
|
the headers get flushed. If this is left as `undefined` then the standard
|
|
|
|
|
message for the status code will be used.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
response.statusMessage = 'Not found';
|
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
After response header was sent to the client, this property indicates the
|
|
|
|
|
status message which was sent out.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2023-02-14 20:55:52 +01:00
|
|
|
|
### `response.strictContentLength`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added:
|
|
|
|
|
- v18.10.0
|
|
|
|
|
- v16.18.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean} **Default:** `false`
|
|
|
|
|
|
|
|
|
|
If set to `true`, Node.js will check whether the `Content-Length`
|
|
|
|
|
header value and the size of the body, in bytes, are equal.
|
|
|
|
|
Mismatching the `Content-Length` header value will result
|
|
|
|
|
in an `Error` being thrown, identified by `code:` [`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.uncork()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-08-08 21:12:41 +02:00
|
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
2019-08-08 21:12:41 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
See [`writable.uncork()`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.writableEnded`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-08-02 08:09:06 +02:00
|
|
|
|
<!-- YAML
|
2019-08-19 21:14:22 +02:00
|
|
|
|
added: v12.9.0
|
2019-08-02 08:09:06 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
Is `true` after [`response.end()`][] has been called. This property
|
|
|
|
|
does not indicate whether the data has been flushed, for this use
|
|
|
|
|
[`response.writableFinished`][] instead.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.writableFinished`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-08-02 19:39:49 +03:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v12.7.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
Is `true` if all data has been flushed to the underlying system, immediately
|
|
|
|
|
before the [`'finish'`][] event is emitted.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.write(chunk[, encoding][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.29
|
2022-10-28 01:45:05 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v15.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33155
|
|
|
|
|
description: The `chunk` parameter can now be a `Uint8Array`.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2022-10-28 01:45:05 +02:00
|
|
|
|
* `chunk` {string|Buffer|Uint8Array}
|
2018-04-02 04:44:32 +03:00
|
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* Returns: {boolean}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
If this method is called and [`response.writeHead()`][] has not been called,
|
2015-11-04 17:56:03 -05:00
|
|
|
|
it will switch to implicit header mode and flush the implicit headers.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
This sends a chunk of the response body. This method may
|
|
|
|
|
be called multiple times to provide successive parts of the body.
|
|
|
|
|
|
2024-06-12 02:09:17 +09:30
|
|
|
|
If `rejectNonStandardBodyWrites` is set to true in `createServer`
|
|
|
|
|
then writing to the body is not allowed when the request method or response
|
|
|
|
|
status do not support content. If an attempt is made to write to the body for a
|
2023-05-13 13:09:26 -04:00
|
|
|
|
HEAD request or as part of a `204` or `304`response, a synchronous `Error`
|
|
|
|
|
with the code `ERR_HTTP_BODY_NOT_ALLOWED` is thrown.
|
2017-04-10 22:57:12 +03:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
`chunk` can be a string or a buffer. If `chunk` is a string,
|
|
|
|
|
the second parameter specifies how to encode it into a byte stream.
|
2018-04-02 04:44:32 +03:00
|
|
|
|
`callback` will be called when this chunk of data is flushed.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-02-05 21:55:16 -08:00
|
|
|
|
This is the raw HTTP body and has nothing to do with higher-level multi-part
|
|
|
|
|
body encodings that may be used.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
The first time [`response.write()`][] is called, it will send the buffered
|
2017-03-03 15:45:20 -08:00
|
|
|
|
header information and the first chunk of the body to the client. The second
|
|
|
|
|
time [`response.write()`][] is called, Node.js assumes data will be streamed,
|
|
|
|
|
and sends the new data separately. That is, the response is buffered up to the
|
|
|
|
|
first chunk of the body.
|
2014-12-18 15:52:42 +08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Returns `true` if the entire data was flushed successfully to the kernel
|
|
|
|
|
buffer. Returns `false` if all or part of the data was queued in user memory.
|
|
|
|
|
`'drain'` will be emitted when the buffer is free again.
|
2011-02-04 15:14:58 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.writeContinue()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
2011-02-04 15:14:58 -08:00
|
|
|
|
|
2022-08-17 20:22:53 +02:00
|
|
|
|
Sends an HTTP/1.1 100 Continue message to the client, indicating that
|
2018-02-12 02:31:55 -05:00
|
|
|
|
the request body should be sent. See the [`'checkContinue'`][] event on
|
|
|
|
|
`Server`.
|
2011-08-12 14:31:17 -07:00
|
|
|
|
|
2022-10-06 18:03:47 +01:00
|
|
|
|
### `response.writeEarlyHints(hints[, callback])`
|
2022-08-17 20:22:53 +02:00
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-10-11 10:37:32 -04:00
|
|
|
|
added: v18.11.0
|
2022-10-06 18:03:47 +01:00
|
|
|
|
changes:
|
2022-10-11 10:37:32 -04:00
|
|
|
|
- version: v18.11.0
|
2022-10-06 18:03:47 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/44820
|
|
|
|
|
description: Allow passing hints as an object.
|
2022-08-17 20:22:53 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2022-10-06 18:03:47 +01:00
|
|
|
|
* `hints` {Object}
|
2022-08-17 20:22:53 +02:00
|
|
|
|
* `callback` {Function}
|
|
|
|
|
|
|
|
|
|
Sends an HTTP/1.1 103 Early Hints message to the client with a Link header,
|
|
|
|
|
indicating that the user agent can preload/preconnect the linked resources.
|
2022-10-06 18:03:47 +01:00
|
|
|
|
The `hints` is an object containing the values of headers to be sent with
|
|
|
|
|
early hints message. The optional `callback` argument will be called when
|
2022-08-17 20:22:53 +02:00
|
|
|
|
the response message has been written.
|
|
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const earlyHintsLink = '</styles.css>; rel=preload; as=style';
|
2022-10-06 18:03:47 +01:00
|
|
|
|
response.writeEarlyHints({
|
|
|
|
|
'link': earlyHintsLink,
|
|
|
|
|
});
|
2022-08-17 20:22:53 +02:00
|
|
|
|
|
|
|
|
|
const earlyHintsLinks = [
|
|
|
|
|
'</styles.css>; rel=preload; as=style',
|
|
|
|
|
'</scripts.js>; rel=preload; as=script',
|
|
|
|
|
];
|
2022-10-06 18:03:47 +01:00
|
|
|
|
response.writeEarlyHints({
|
|
|
|
|
'link': earlyHintsLinks,
|
2022-11-17 08:19:12 -05:00
|
|
|
|
'x-trace-id': 'id for diagnostics',
|
2022-10-06 18:03:47 +01:00
|
|
|
|
});
|
2022-08-17 20:22:53 +02:00
|
|
|
|
|
|
|
|
|
const earlyHintsCallback = () => console.log('early hints message sent');
|
2022-10-17 15:57:28 +01:00
|
|
|
|
response.writeEarlyHints({
|
|
|
|
|
'link': earlyHintsLinks,
|
|
|
|
|
}, earlyHintsCallback);
|
2022-08-17 20:22:53 +02:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.writeHead(statusCode[, statusMessage][, headers])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.30
|
2017-02-21 23:38:45 +01:00
|
|
|
|
changes:
|
2020-10-14 16:55:05 -04:00
|
|
|
|
- version: v14.14.0
|
2020-09-20 09:32:06 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/35274
|
|
|
|
|
description: Allow passing headers as an array.
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v11.10.0
|
|
|
|
|
- v10.17.0
|
2019-02-06 22:17:23 +00:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/25974
|
|
|
|
|
description: Return `this` from `writeHead()` to allow chaining with
|
|
|
|
|
`end()`.
|
2020-10-01 20:23:33 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v5.11.0
|
|
|
|
|
- v4.4.5
|
2017-02-21 23:38:45 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6291
|
|
|
|
|
description: A `RangeError` is thrown if `statusCode` is not a number in
|
|
|
|
|
the range `[100, 999]`.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-08-12 14:31:17 -07:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `statusCode` {number}
|
|
|
|
|
* `statusMessage` {string}
|
2020-09-20 09:32:06 +02:00
|
|
|
|
* `headers` {Object|Array}
|
2019-02-06 22:17:23 +00:00
|
|
|
|
* Returns: {http.ServerResponse}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Sends a response header to the request. The status code is a 3-digit HTTP
|
|
|
|
|
status code, like `404`. The last argument, `headers`, are the response headers.
|
|
|
|
|
Optionally one can give a human-readable `statusMessage` as the second
|
|
|
|
|
argument.
|
2015-05-13 21:25:57 -05:00
|
|
|
|
|
2020-09-20 09:32:06 +02:00
|
|
|
|
`headers` may be an `Array` where the keys and values are in the same list.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
It is _not_ a list of tuples. So, the even-numbered offsets are key values,
|
2020-09-20 09:32:06 +02:00
|
|
|
|
and the odd-numbered offsets are the associated values. The array is in the same
|
|
|
|
|
format as `request.rawHeaders`.
|
|
|
|
|
|
2019-02-06 22:17:23 +00:00
|
|
|
|
Returns a reference to the `ServerResponse`, so that calls can be chained.
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const body = 'hello world';
|
2019-02-06 22:17:23 +00:00
|
|
|
|
response
|
|
|
|
|
.writeHead(200, {
|
|
|
|
|
'Content-Length': Buffer.byteLength(body),
|
2022-11-17 08:19:12 -05:00
|
|
|
|
'Content-Type': 'text/plain',
|
2019-02-06 22:17:23 +00:00
|
|
|
|
})
|
|
|
|
|
.end(body);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2011-08-12 14:31:17 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
This method must only be called once on a message and it must
|
2015-11-27 18:30:32 -05:00
|
|
|
|
be called before [`response.end()`][] is called.
|
2011-08-12 14:31:17 -07:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
If [`response.write()`][] or [`response.end()`][] are called before calling
|
|
|
|
|
this, the implicit/mutable headers will be calculated and call this function.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
When headers have been set with [`response.setHeader()`][], they will be merged
|
|
|
|
|
with any headers passed to [`response.writeHead()`][], with the headers passed
|
|
|
|
|
to [`response.writeHead()`][] given precedence.
|
2016-02-05 10:48:41 -03:00
|
|
|
|
|
2018-06-12 10:41:07 -04:00
|
|
|
|
If this method is called and [`response.setHeader()`][] has not been called,
|
|
|
|
|
it will directly write the supplied header values onto the network channel
|
|
|
|
|
without caching internally, and the [`response.getHeader()`][] on the header
|
|
|
|
|
will not yield the expected result. If progressive population of headers is
|
|
|
|
|
desired with potential future retrieval and modification, use
|
|
|
|
|
[`response.setHeader()`][] instead.
|
|
|
|
|
|
2016-02-05 10:48:41 -03:00
|
|
|
|
```js
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Returns content-type = text/plain
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const server = http.createServer((req, res) => {
|
2016-02-05 10:48:41 -03:00
|
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
|
res.setHeader('X-Foo', 'bar');
|
2017-06-01 02:07:25 +03:00
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-02-05 10:48:41 -03:00
|
|
|
|
res.end('ok');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2022-08-24 22:19:29 +05:30
|
|
|
|
`Content-Length` is read in bytes, not characters. Use
|
2020-04-06 22:08:33 -07:00
|
|
|
|
[`Buffer.byteLength()`][] to determine the length of the body in bytes. Node.js
|
2022-08-24 22:19:29 +05:30
|
|
|
|
will check whether `Content-Length` and the length of the body which has
|
2020-04-06 22:08:33 -07:00
|
|
|
|
been transmitted are equal or not.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-02-03 17:32:23 -08:00
|
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
2022-08-24 22:19:29 +05:30
|
|
|
|
will result in a \[`Error`]\[] being thrown.
|
2016-02-03 17:32:23 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `response.writeProcessing()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-01-08 00:11:05 -08:00
|
|
|
|
<!-- YAML
|
2018-03-02 09:53:46 -08:00
|
|
|
|
added: v10.0.0
|
2018-01-08 00:11:05 -08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Sends a HTTP/1.1 102 Processing message to the client, indicating that
|
|
|
|
|
the request body should be sent.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## Class: `http.IncomingMessage`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.17
|
2019-10-25 21:17:06 -04:00
|
|
|
|
changes:
|
2020-12-27 20:45:36 +08:00
|
|
|
|
- version: v15.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33035
|
|
|
|
|
description: The `destroyed` value returns `true` after the incoming data
|
|
|
|
|
is consumed.
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.1.0
|
|
|
|
|
- v12.16.0
|
2019-10-25 21:17:06 -04:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30135
|
|
|
|
|
description: The `readableHighWaterMark` value mirrors that of the socket.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2019-08-21 13:11:16 -07:00
|
|
|
|
* Extends: {stream.Readable}
|
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
An `IncomingMessage` object is created by [`http.Server`][] or
|
2016-09-10 16:03:30 -07:00
|
|
|
|
[`http.ClientRequest`][] and passed as the first argument to the [`'request'`][]
|
2018-02-12 02:31:55 -05:00
|
|
|
|
and [`'response'`][] event respectively. It may be used to access response
|
2022-05-10 00:49:22 +02:00
|
|
|
|
status, headers, and data.
|
2012-02-05 19:11:54 +09:00
|
|
|
|
|
2021-01-13 16:22:14 +01:00
|
|
|
|
Different from its `socket` value which is a subclass of {stream.Duplex}, the
|
2020-12-27 20:45:36 +08:00
|
|
|
|
`IncomingMessage` itself extends {stream.Readable} and is created separately to
|
|
|
|
|
parse and emit the incoming HTTP headers and payload, as the underlying socket
|
|
|
|
|
may be reused multiple times in case of keep-alive.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'aborted'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.8
|
2021-10-18 12:00:42 -04:00
|
|
|
|
deprecated:
|
|
|
|
|
- v17.0.0
|
|
|
|
|
- v16.12.0
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2016-06-10 15:41:36 -07:00
|
|
|
|
|
2021-01-19 07:55:57 +08:00
|
|
|
|
> Stability: 0 - Deprecated. Listen for `'close'` event instead.
|
|
|
|
|
|
2018-04-17 11:37:50 +02:00
|
|
|
|
Emitted when the request has been aborted.
|
2016-06-10 15:41:36 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### Event: `'close'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.2
|
2022-04-01 15:58:14 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version: v16.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33035
|
|
|
|
|
description: The close event is now emitted when the request has been completed and not when the
|
|
|
|
|
underlying socket is closed.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2011-06-11 22:09:40 +09:00
|
|
|
|
|
2022-04-01 15:58:14 +02:00
|
|
|
|
Emitted when the request has been completed.
|
2012-10-12 15:27:47 +02:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.aborted`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-04-17 11:37:50 +02:00
|
|
|
|
<!-- YAML
|
2018-05-08 09:11:24 -07:00
|
|
|
|
added: v10.1.0
|
2021-10-18 12:00:42 -04:00
|
|
|
|
deprecated:
|
|
|
|
|
- v17.0.0
|
|
|
|
|
- v16.12.0
|
2018-04-17 11:37:50 +02:00
|
|
|
|
-->
|
|
|
|
|
|
2021-01-19 07:55:57 +08:00
|
|
|
|
> Stability: 0 - Deprecated. Check `message.destroyed` from {stream.Readable}.
|
|
|
|
|
|
2018-04-17 11:37:50 +02:00
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
The `message.aborted` property will be `true` if the request has
|
|
|
|
|
been aborted.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.complete`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-10-26 15:09:16 -07:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
The `message.complete` property will be `true` if a complete HTTP message has
|
|
|
|
|
been received and successfully parsed.
|
|
|
|
|
|
|
|
|
|
This property is particularly useful as a means of determining if a client or
|
|
|
|
|
server fully transmitted a message before a connection was terminated:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const req = http.request({
|
|
|
|
|
host: '127.0.0.1',
|
|
|
|
|
port: 8080,
|
2022-11-17 08:19:12 -05:00
|
|
|
|
method: 'POST',
|
2018-10-26 15:09:16 -07:00
|
|
|
|
}, (res) => {
|
|
|
|
|
res.resume();
|
|
|
|
|
res.on('end', () => {
|
|
|
|
|
if (!res.complete)
|
|
|
|
|
console.error(
|
|
|
|
|
'The connection was terminated while the message was still being sent');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2020-06-06 11:57:04 +05:30
|
|
|
|
### `message.connection`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-06-06 11:57:04 +05:30
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
2021-03-03 15:36:13 +00:00
|
|
|
|
deprecated: v16.0.0
|
2020-06-06 11:57:04 +05:30
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
> Stability: 0 - Deprecated. Use [`message.socket`][].
|
|
|
|
|
|
|
|
|
|
Alias for [`message.socket`][].
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.destroy([error])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
2020-04-11 16:06:43 -04:00
|
|
|
|
changes:
|
2020-09-28 10:54:13 -07:00
|
|
|
|
- version:
|
|
|
|
|
- v14.5.0
|
|
|
|
|
- v12.19.0
|
2020-04-11 16:06:43 -04:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/32789
|
|
|
|
|
description: The function returns `this` for consistency with other Readable
|
|
|
|
|
streams.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2016-06-08 16:23:21 -07:00
|
|
|
|
|
|
|
|
|
* `error` {Error}
|
2020-04-11 16:06:43 -04:00
|
|
|
|
* Returns: {this}
|
2016-06-08 16:23:21 -07:00
|
|
|
|
|
|
|
|
|
Calls `destroy()` on the socket that received the `IncomingMessage`. If `error`
|
2019-11-04 17:45:27 -05:00
|
|
|
|
is provided, an `'error'` event is emitted on the socket and `error` is passed
|
|
|
|
|
as an argument to any listeners on the event.
|
2016-06-08 16:23:21 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.headers`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.5
|
2020-12-22 14:00:00 +01:00
|
|
|
|
changes:
|
2023-01-28 16:47:47 -05:00
|
|
|
|
- version:
|
|
|
|
|
- v19.5.0
|
|
|
|
|
- v18.14.0
|
2023-01-03 11:43:21 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/45982
|
|
|
|
|
description: >-
|
|
|
|
|
The `joinDuplicateHeaders` option in the `http.request()`
|
|
|
|
|
and `http.createServer()` functions ensures that duplicate
|
|
|
|
|
headers are not discarded, but rather combined using a
|
|
|
|
|
comma separator, in accordance with RFC 9110 Section 5.3.
|
2020-12-22 14:00:00 +01:00
|
|
|
|
- version: v15.1.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/35281
|
|
|
|
|
description: >-
|
|
|
|
|
`message.headers` is now lazily computed using an accessor property
|
2022-02-23 18:16:50 +00:00
|
|
|
|
on the prototype and is no longer enumerable.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* {Object}
|
|
|
|
|
|
2013-01-19 15:49:30 -08:00
|
|
|
|
The request/response headers object.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-13 10:01:34 -05:00
|
|
|
|
Key-value pairs of header names and values. Header names are lower-cased.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
// Prints something like:
|
|
|
|
|
//
|
|
|
|
|
// { 'user-agent': 'curl/7.22.0',
|
|
|
|
|
// host: '127.0.0.1:8000',
|
|
|
|
|
// accept: '*/*' }
|
2022-09-12 21:29:30 +02:00
|
|
|
|
console.log(request.headers);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2015-11-12 23:21:22 -08:00
|
|
|
|
Duplicates in raw headers are handled in the following ways, depending on the
|
|
|
|
|
header name:
|
|
|
|
|
|
|
|
|
|
* Duplicates of `age`, `authorization`, `content-length`, `content-type`,
|
2020-11-09 05:44:32 -08:00
|
|
|
|
`etag`, `expires`, `from`, `host`, `if-modified-since`, `if-unmodified-since`,
|
|
|
|
|
`last-modified`, `location`, `max-forwards`, `proxy-authorization`, `referer`,
|
|
|
|
|
`retry-after`, `server`, or `user-agent` are discarded.
|
2023-01-03 11:43:21 +01:00
|
|
|
|
To allow duplicate values of the headers listed above to be joined,
|
|
|
|
|
use the option `joinDuplicateHeaders` in [`http.request()`][]
|
|
|
|
|
and [`http.createServer()`][]. See RFC 9110 Section 5.3 for more
|
|
|
|
|
information.
|
2015-11-12 23:21:22 -08:00
|
|
|
|
* `set-cookie` is always an array. Duplicates are added to the array.
|
2022-01-10 21:50:58 +01:00
|
|
|
|
* For duplicate `cookie` headers, the values are joined together with `; `.
|
|
|
|
|
* For all other headers, the values are joined together with `, `.
|
|
|
|
|
|
|
|
|
|
### `message.headersDistinct`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
|
added:
|
|
|
|
|
- v18.3.0
|
|
|
|
|
- v16.17.0
|
2022-01-10 21:50:58 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {Object}
|
|
|
|
|
|
|
|
|
|
Similar to [`message.headers`][], but there is no join logic and the values are
|
|
|
|
|
always arrays of strings, even for headers received just once.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Prints something like:
|
|
|
|
|
//
|
|
|
|
|
// { 'user-agent': ['curl/7.22.0'],
|
|
|
|
|
// host: ['127.0.0.1:8000'],
|
|
|
|
|
// accept: ['*/*'] }
|
|
|
|
|
console.log(request.headersDistinct);
|
|
|
|
|
```
|
2015-11-12 23:21:22 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.httpVersion`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.1
|
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
In case of server request, the HTTP version sent by the client. In the case of
|
|
|
|
|
client response, the HTTP version of the connected-to server.
|
|
|
|
|
Probably either `'1.1'` or `'1.0'`.
|
|
|
|
|
|
2016-02-18 17:15:28 +08:00
|
|
|
|
Also `message.httpVersionMajor` is the first integer and
|
|
|
|
|
`message.httpVersionMinor` is the second.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.method`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.1
|
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
**Only valid for request obtained from [`http.Server`][].**
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2018-08-26 19:02:27 +03:00
|
|
|
|
The request method as a string. Read only. Examples: `'GET'`, `'DELETE'`.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.rawHeaders`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.6
|
|
|
|
|
-->
|
2013-08-15 14:12:12 -07:00
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
|
* {string\[]}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2013-08-15 14:12:12 -07:00
|
|
|
|
The raw request/response headers list exactly as they were received.
|
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
|
The keys and values are in the same list. It is _not_ a
|
2018-04-02 08:38:48 +03:00
|
|
|
|
list of tuples. So, the even-numbered offsets are key values, and the
|
2013-08-15 14:12:12 -07:00
|
|
|
|
odd-numbered offsets are the associated values.
|
|
|
|
|
|
|
|
|
|
Header names are not lowercased, and duplicates are not merged.
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
// Prints something like:
|
|
|
|
|
//
|
|
|
|
|
// [ 'user-agent',
|
|
|
|
|
// 'this is invalid because there can be only one',
|
|
|
|
|
// 'User-Agent',
|
|
|
|
|
// 'curl/7.22.0',
|
|
|
|
|
// 'Host',
|
|
|
|
|
// '127.0.0.1:8000',
|
|
|
|
|
// 'ACCEPT',
|
|
|
|
|
// '*/*' ]
|
|
|
|
|
console.log(request.rawHeaders);
|
|
|
|
|
```
|
2013-08-15 14:12:12 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.rawTrailers`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.6
|
|
|
|
|
-->
|
2013-08-15 14:12:12 -07:00
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
|
* {string\[]}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2013-08-15 14:12:12 -07:00
|
|
|
|
The raw request/response trailer keys and values exactly as they were
|
2018-04-02 08:38:48 +03:00
|
|
|
|
received. Only populated at the `'end'` event.
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.setTimeout(msecs[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
|
|
|
|
-->
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `msecs` {number}
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
* `callback` {Function}
|
2018-04-11 21:07:14 +03:00
|
|
|
|
* Returns: {http.IncomingMessage}
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2020-10-01 10:43:13 +02:00
|
|
|
|
Calls `message.socket.setTimeout(msecs, callback)`.
|
http: More useful setTimeout API on server
This adds the following to HTTP:
* server.setTimeout(msecs, callback)
Sets all new connections to time out after the specified time, at
which point it emits 'timeout' on the server, passing the socket as an
argument.
In this way, timeouts can be handled in one place consistently.
* req.setTimeout(), res.setTimeout()
Essentially an alias to req/res.socket.setTimeout(), but without
having to delve into a "buried" object. Adds a listener on the
req/res object, but not on the socket.
* server.timeout
Number of milliseconds before incoming connections time out.
(Default=1000*60*2, as before.)
Furthermore, if the user sets up their own timeout listener on either
the server, the request, or the response, then the default behavior
(destroying the socket) is suppressed.
Fix #3460
2013-03-03 23:29:22 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.socket`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2017-02-13 04:49:35 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
* {stream.Duplex}
|
2017-02-13 04:49:35 +02:00
|
|
|
|
|
|
|
|
|
The [`net.Socket`][] object associated with the connection.
|
|
|
|
|
|
|
|
|
|
With HTTPS support, use [`request.socket.getPeerCertificate()`][] to obtain the
|
|
|
|
|
client's authentication details.
|
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
|
This property is guaranteed to be an instance of the {net.Socket} class,
|
|
|
|
|
a subclass of {stream.Duplex}, unless the user specified a socket
|
2021-12-01 20:47:20 +01:00
|
|
|
|
type other than {net.Socket} or internally nulled.
|
2019-10-28 16:47:07 +01:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.statusCode`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.1
|
|
|
|
|
-->
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {number}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
**Only valid for response obtained from [`http.ClientRequest`][].**
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
The 3-digit HTTP response status code. E.G. `404`.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.statusMessage`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.10
|
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
**Only valid for response obtained from [`http.ClientRequest`][].**
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
|
The HTTP response status message (reason phrase). E.G. `OK` or `Internal Server
|
|
|
|
|
Error`.
|
2016-01-22 14:45:12 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.trailers`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* {Object}
|
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
The request/response trailers object. Only populated at the `'end'` event.
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2022-01-10 21:50:58 +01:00
|
|
|
|
### `message.trailersDistinct`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
|
added:
|
|
|
|
|
- v18.3.0
|
|
|
|
|
- v16.17.0
|
2022-01-10 21:50:58 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {Object}
|
|
|
|
|
|
|
|
|
|
Similar to [`message.trailers`][], but there is no join logic and the values are
|
|
|
|
|
always arrays of strings, even for headers received just once.
|
|
|
|
|
Only populated at the `'end'` event.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
### `message.url`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
|
|
|
|
-->
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2017-03-03 15:45:20 -08:00
|
|
|
|
* {string}
|
2016-09-10 16:03:30 -07:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
**Only valid for request obtained from [`http.Server`][].**
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2020-06-07 17:21:45 -04:00
|
|
|
|
Request URL string. This contains only the URL that is present in the actual
|
2020-07-02 16:36:38 -07:00
|
|
|
|
HTTP request. Take the following request:
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2020-04-23 11:01:52 -07:00
|
|
|
|
```http
|
2020-06-07 17:21:45 -04:00
|
|
|
|
GET /status?name=ryan HTTP/1.1
|
|
|
|
|
Accept: text/plain
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2019-12-07 13:20:20 +09:00
|
|
|
|
To parse the URL into its parts:
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2016-07-09 08:13:09 +03:00
|
|
|
|
```js
|
2024-04-21 14:56:11 +02:00
|
|
|
|
new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2024-04-21 14:56:11 +02:00
|
|
|
|
When `request.url` is `'/status?name=ryan'` and `process.env.HOST` is undefined:
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2019-09-01 11:07:24 +08:00
|
|
|
|
```console
|
2016-01-21 23:21:22 +01:00
|
|
|
|
$ node
|
2024-04-21 14:56:11 +02:00
|
|
|
|
> new URL(`http://${process.env.HOST ?? 'localhost'}${request.url}`);
|
2019-12-07 13:20:20 +09:00
|
|
|
|
URL {
|
2024-04-21 14:56:11 +02:00
|
|
|
|
href: 'http://localhost/status?name=ryan',
|
|
|
|
|
origin: 'http://localhost',
|
2019-12-07 13:20:20 +09:00
|
|
|
|
protocol: 'http:',
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
2024-04-21 14:56:11 +02:00
|
|
|
|
host: 'localhost',
|
2019-12-07 13:20:20 +09:00
|
|
|
|
hostname: 'localhost',
|
2024-04-21 14:56:11 +02:00
|
|
|
|
port: '',
|
2017-04-02 21:39:42 +03:00
|
|
|
|
pathname: '/status',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
search: '?name=ryan',
|
2019-12-07 13:20:20 +09:00
|
|
|
|
searchParams: URLSearchParams { 'name' => 'ryan' },
|
|
|
|
|
hash: ''
|
|
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
|
2024-04-21 14:56:11 +02:00
|
|
|
|
Ensure that you set `process.env.HOST` to the server's host name, or consider
|
|
|
|
|
replacing this part entirely. If using `req.headers.host`, ensure proper
|
|
|
|
|
validation is used, as clients may specify a custom `Host` header.
|
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
## Class: `http.OutgoingMessage`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.17
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Extends: {Stream}
|
|
|
|
|
|
|
|
|
|
This class serves as the parent class of [`http.ClientRequest`][]
|
2022-04-17 21:23:55 +02:00
|
|
|
|
and [`http.ServerResponse`][]. It is an abstract outgoing message from
|
|
|
|
|
the perspective of the participants of an HTTP transaction.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-02-28 23:56:54 -05:00
|
|
|
|
### Event: `'drain'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Emitted when the buffer of the message is free again.
|
|
|
|
|
|
2022-02-28 23:56:54 -05:00
|
|
|
|
### Event: `'finish'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.17
|
|
|
|
|
-->
|
|
|
|
|
|
2021-02-24 20:56:54 +08:00
|
|
|
|
Emitted when the transmission is finished successfully.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-02-28 23:56:54 -05:00
|
|
|
|
### Event: `'prefinish'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.6
|
|
|
|
|
-->
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Emitted after `outgoingMessage.end()` is called.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
When the event is emitted, all data has been processed but not necessarily
|
|
|
|
|
completely flushed.
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.addTrailers(headers)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `headers` {Object}
|
|
|
|
|
|
|
|
|
|
Adds HTTP trailers (headers but at the end of the message) to the message.
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Trailers will **only** be emitted if the message is chunked encoded. If not,
|
|
|
|
|
the trailers will be silently discarded.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-01-10 21:50:58 +01:00
|
|
|
|
HTTP requires the `Trailer` header to be sent to emit trailers,
|
2022-04-17 21:23:55 +02:00
|
|
|
|
with a list of header field names in its value, e.g.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
message.writeHead(200, { 'Content-Type': 'text/plain',
|
|
|
|
|
'Trailer': 'Content-MD5' });
|
|
|
|
|
message.write(fileData);
|
|
|
|
|
message.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' });
|
|
|
|
|
message.end();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
|
|
|
|
will result in a `TypeError` being thrown.
|
|
|
|
|
|
2022-01-10 21:50:58 +01:00
|
|
|
|
### `outgoingMessage.appendHeader(name, value)`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
|
added:
|
|
|
|
|
- v18.3.0
|
|
|
|
|
- v16.17.0
|
2022-01-10 21:50:58 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string} Header name
|
|
|
|
|
* `value` {string|string\[]} Header value
|
|
|
|
|
* Returns: {this}
|
|
|
|
|
|
2024-01-12 17:09:57 +01:00
|
|
|
|
Append a single header value to the header object.
|
2022-01-10 21:50:58 +01:00
|
|
|
|
|
2024-01-12 17:09:57 +01:00
|
|
|
|
If the value is an array, this is equivalent to calling this method multiple
|
2022-01-10 21:50:58 +01:00
|
|
|
|
times.
|
|
|
|
|
|
2024-01-12 17:09:57 +01:00
|
|
|
|
If there were no previous values for the header, this is equivalent to calling
|
2022-01-10 21:50:58 +01:00
|
|
|
|
[`outgoingMessage.setHeader(name, value)`][].
|
|
|
|
|
|
|
|
|
|
Depending of the value of `options.uniqueHeaders` when the client request or the
|
|
|
|
|
server were created, this will end up in the header being sent multiple times or
|
|
|
|
|
a single time with values joined using `; `.
|
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
### `outgoingMessage.connection`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
2021-06-05 11:13:59 +02:00
|
|
|
|
deprecated:
|
|
|
|
|
- v15.12.0
|
|
|
|
|
- v14.17.1
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
> Stability: 0 - Deprecated: Use [`outgoingMessage.socket`][] instead.
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Alias of [`outgoingMessage.socket`][].
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
### `outgoingMessage.cork()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-15 00:09:04 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
See [`writable.cork()`][].
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.destroy([error])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `error` {Error} Optional, an error to emit with `error` event
|
|
|
|
|
* Returns: {this}
|
|
|
|
|
|
|
|
|
|
Destroys the message. Once a socket is associated with the message
|
|
|
|
|
and is connected, that socket will be destroyed as well.
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.end(chunk[, encoding][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
|
|
|
|
changes:
|
2022-10-28 01:45:05 +02:00
|
|
|
|
- version: v15.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33155
|
|
|
|
|
description: The `chunk` parameter can now be a `Uint8Array`.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
- version: v0.11.6
|
|
|
|
|
description: add `callback` argument.
|
|
|
|
|
-->
|
|
|
|
|
|
2022-10-28 01:45:05 +02:00
|
|
|
|
* `chunk` {string|Buffer|Uint8Array}
|
2021-03-27 14:26:39 +01:00
|
|
|
|
* `encoding` {string} Optional, **Default**: `utf8`
|
2021-02-04 18:53:57 +08:00
|
|
|
|
* `callback` {Function} Optional
|
|
|
|
|
* Returns: {this}
|
|
|
|
|
|
|
|
|
|
Finishes the outgoing message. If any parts of the body are unsent, it will
|
|
|
|
|
flush them to the underlying system. If the message is chunked, it will
|
2022-04-17 21:23:55 +02:00
|
|
|
|
send the terminating chunk `0\r\n\r\n`, and send the trailers (if any).
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
If `chunk` is specified, it is equivalent to calling
|
2021-02-04 18:53:57 +08:00
|
|
|
|
`outgoingMessage.write(chunk, encoding)`, followed by
|
|
|
|
|
`outgoingMessage.end(callback)`.
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
If `callback` is provided, it will be called when the message is finished
|
|
|
|
|
(equivalent to a listener of the `'finish'` event).
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.flushHeaders()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v1.6.0
|
|
|
|
|
-->
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Flushes the message headers.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
For efficiency reason, Node.js normally buffers the message headers
|
|
|
|
|
until `outgoingMessage.end()` is called or the first chunk of message data
|
|
|
|
|
is written. It then tries to pack the headers and data into a single TCP
|
|
|
|
|
packet.
|
|
|
|
|
|
|
|
|
|
It is usually desired (it saves a TCP round-trip), but not when the first
|
|
|
|
|
data is not sent until possibly much later. `outgoingMessage.flushHeaders()`
|
2022-04-17 21:23:55 +02:00
|
|
|
|
bypasses the optimization and kickstarts the message.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.getHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string} Name of header
|
2024-02-14 00:37:42 +03:00
|
|
|
|
* Returns: {string | undefined}
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Gets the value of the HTTP header with the given name. If that header is not
|
|
|
|
|
set, the returned value will be `undefined`.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.getHeaderNames()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-11 07:21:48 +02:00
|
|
|
|
added: v7.7.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
2024-02-14 00:37:42 +03:00
|
|
|
|
* Returns: {string\[]}
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Returns an array containing the unique names of the current outgoing headers.
|
|
|
|
|
All names are lowercase.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.getHeaders()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-11 07:21:48 +02:00
|
|
|
|
added: v7.7.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* Returns: {Object}
|
|
|
|
|
|
|
|
|
|
Returns a shallow copy of the current outgoing headers. Since a shallow
|
|
|
|
|
copy is used, array values may be mutated without additional calls to
|
2021-02-24 20:56:54 +08:00
|
|
|
|
various header-related HTTP module methods. The keys of the returned
|
2021-02-04 18:53:57 +08:00
|
|
|
|
object are the header names and the values are the respective header
|
|
|
|
|
values. All header names are lowercase.
|
|
|
|
|
|
|
|
|
|
The object returned by the `outgoingMessage.getHeaders()` method does
|
2022-04-17 21:23:55 +02:00
|
|
|
|
not prototypically inherit from the JavaScript `Object`. This means that
|
|
|
|
|
typical `Object` methods such as `obj.toString()`, `obj.hasOwnProperty()`,
|
2021-02-04 18:53:57 +08:00
|
|
|
|
and others are not defined and will not work.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
outgoingMessage.setHeader('Foo', 'bar');
|
|
|
|
|
outgoingMessage.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
|
|
|
|
|
const headers = outgoingMessage.getHeaders();
|
|
|
|
|
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.hasHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-11 07:21:48 +02:00
|
|
|
|
added: v7.7.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
2024-02-14 00:37:42 +03:00
|
|
|
|
* Returns: {boolean}
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
Returns `true` if the header identified by `name` is currently set in the
|
|
|
|
|
outgoing headers. The header name is case-insensitive.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const hasContentType = outgoingMessage.hasHeader('content-type');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.headersSent`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.3
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
|
|
|
|
Read-only. `true` if the headers were sent, otherwise `false`.
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.pipe()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v9.0.0
|
|
|
|
|
-->
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Overrides the `stream.pipe()` method inherited from the legacy `Stream` class
|
|
|
|
|
which is the parent class of `http.OutgoingMessage`.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Calling this method will throw an `Error` because `outgoingMessage` is a
|
|
|
|
|
write-only stream.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-10 10:08:56 +02:00
|
|
|
|
### `outgoingMessage.removeHeader(name)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-10 10:08:56 +02:00
|
|
|
|
added: v0.4.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
2022-04-10 10:08:56 +02:00
|
|
|
|
* `name` {string} Header name
|
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
Removes a header that is queued for implicit sending.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
outgoingMessage.removeHeader('Content-Encoding');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.setHeader(name, value)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string} Header name
|
2022-04-17 21:23:55 +02:00
|
|
|
|
* `value` {any} Header value
|
2021-02-04 18:53:57 +08:00
|
|
|
|
* Returns: {this}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Sets a single header value. If the header already exists in the to-be-sent
|
|
|
|
|
headers, its value will be replaced. Use an array of strings to send multiple
|
|
|
|
|
headers with the same name.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2023-01-09 20:15:49 +01:00
|
|
|
|
### `outgoingMessage.setHeaders(headers)`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-03-04 23:12:52 -05:00
|
|
|
|
added:
|
|
|
|
|
- v19.6.0
|
|
|
|
|
- v18.15.0
|
2023-01-09 20:15:49 +01:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `headers` {Headers|Map}
|
2024-10-11 21:03:35 +08:00
|
|
|
|
* Returns: {this}
|
2023-01-09 20:15:49 +01:00
|
|
|
|
|
|
|
|
|
Sets multiple header values for implicit headers.
|
|
|
|
|
`headers` must be an instance of [`Headers`][] or `Map`,
|
|
|
|
|
if a header already exists in the to-be-sent headers,
|
|
|
|
|
its value will be replaced.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const headers = new Headers({ foo: 'bar' });
|
2024-10-11 21:03:35 +08:00
|
|
|
|
outgoingMessage.setHeaders(headers);
|
2023-01-09 20:15:49 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const headers = new Map([['foo', 'bar']]);
|
2024-10-11 21:03:35 +08:00
|
|
|
|
outgoingMessage.setHeaders(headers);
|
2023-01-09 20:15:49 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
When headers have been set with [`outgoingMessage.setHeaders()`][],
|
|
|
|
|
they will be merged with any headers passed to [`response.writeHead()`][],
|
|
|
|
|
with the headers passed to [`response.writeHead()`][] given precedence.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Returns content-type = text/plain
|
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
|
const headers = new Headers({ 'Content-Type': 'text/html' });
|
|
|
|
|
res.setHeaders(headers);
|
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
|
|
|
res.end('ok');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
### `outgoingMessage.setTimeout(msesc[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.12
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `msesc` {number}
|
|
|
|
|
* `callback` {Function} Optional function to be called when a timeout
|
2021-09-16 21:56:24 -07:00
|
|
|
|
occurs. Same as binding to the `timeout` event.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
* Returns: {this}
|
|
|
|
|
|
|
|
|
|
Once a socket is associated with the message and is connected,
|
|
|
|
|
[`socket.setTimeout()`][] will be called with `msecs` as the first parameter.
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.socket`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.0
|
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {stream.Duplex}
|
|
|
|
|
|
2021-02-24 20:56:54 +08:00
|
|
|
|
Reference to the underlying socket. Usually, users will not want to access
|
2021-02-04 18:53:57 +08:00
|
|
|
|
this property.
|
|
|
|
|
|
|
|
|
|
After calling `outgoingMessage.end()`, this property will be nulled.
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.uncork()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-15 00:09:04 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
See [`writable.uncork()`][]
|
|
|
|
|
|
|
|
|
|
### `outgoingMessage.writableCorked`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-17 08:31:47 +02:00
|
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {number}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
The number of times `outgoingMessage.cork()` has been called.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.writableEnded`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-17 08:31:47 +02:00
|
|
|
|
added: v12.9.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Is `true` if `outgoingMessage.end()` has been called. This property does
|
|
|
|
|
not indicate whether the data has been flushed. For that purpose, use
|
|
|
|
|
`message.writableFinished` instead.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.writableFinished`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-17 08:31:47 +02:00
|
|
|
|
added: v12.7.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Is `true` if all data has been flushed to the underlying system.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.writableHighWaterMark`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-17 08:31:47 +02:00
|
|
|
|
added: v12.9.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {number}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
The `highWaterMark` of the underlying socket if assigned. Otherwise, the default
|
|
|
|
|
buffer level when [`writable.write()`][] starts returning false (`16384`).
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.writableLength`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-17 08:31:47 +02:00
|
|
|
|
added: v12.9.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {number}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
The number of buffered bytes.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.writableObjectMode`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
2022-04-17 08:31:47 +02:00
|
|
|
|
added: v12.9.0
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {boolean}
|
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Always `false`.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
### `outgoingMessage.write(chunk[, encoding][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2021-02-04 18:53:57 +08:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.29
|
|
|
|
|
changes:
|
2022-10-28 01:45:05 +02:00
|
|
|
|
- version: v15.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/33155
|
|
|
|
|
description: The `chunk` parameter can now be a `Uint8Array`.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
- version: v0.11.6
|
2022-04-17 21:23:55 +02:00
|
|
|
|
description: The `callback` argument was added.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
-->
|
|
|
|
|
|
2022-10-28 01:45:05 +02:00
|
|
|
|
* `chunk` {string|Buffer|Uint8Array}
|
2021-03-27 14:26:39 +01:00
|
|
|
|
* `encoding` {string} **Default**: `utf8`
|
2021-02-04 18:53:57 +08:00
|
|
|
|
* `callback` {Function}
|
2024-02-14 00:37:42 +03:00
|
|
|
|
* Returns: {boolean}
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
Sends a chunk of the body. This method can be called multiple times.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
The `encoding` argument is only relevant when `chunk` is a string. Defaults to
|
|
|
|
|
`'utf8'`.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2022-04-17 21:23:55 +02:00
|
|
|
|
The `callback` argument is optional and will be called when this chunk of data
|
|
|
|
|
is flushed.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
|
|
|
|
Returns `true` if the entire data was flushed successfully to the kernel
|
2021-02-24 20:56:54 +08:00
|
|
|
|
buffer. Returns `false` if all or part of the data was queued in the user
|
2022-04-17 21:23:55 +02:00
|
|
|
|
memory. The `'drain'` event will be emitted when the buffer is free again.
|
2021-02-04 18:53:57 +08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.METHODS`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.8
|
|
|
|
|
-->
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
|
* {string\[]}
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
A list of the HTTP methods that are supported by the parser.
|
2013-01-19 15:49:30 -08:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.STATUS_CODES`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.22
|
|
|
|
|
-->
|
2013-09-23 00:06:58 +10:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
* {Object}
|
2013-09-23 00:06:58 +10:00
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
A collection of all the standard HTTP response status codes, and the
|
2018-04-02 08:38:48 +03:00
|
|
|
|
short description of each. For example, `http.STATUS_CODES[404] === 'Not
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Found'`.
|
2013-09-23 00:06:58 +10:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.createServer([options][, requestListener])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.13
|
2018-02-06 22:34:50 +01:00
|
|
|
|
changes:
|
2023-07-10 08:12:52 -04:00
|
|
|
|
- version:
|
|
|
|
|
- v20.1.0
|
|
|
|
|
- v18.17.0
|
2023-04-24 13:35:38 +08:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/47405
|
|
|
|
|
description: The `highWaterMark` option is supported now.
|
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
|
|
|
|
- version: v18.0.0
|
2022-04-13 16:47:59 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/41263
|
2022-05-10 00:49:22 +02:00
|
|
|
|
description: The `requestTimeout`, `headersTimeout`, `keepAliveTimeout`, and
|
|
|
|
|
`connectionsCheckingInterval` options are supported now.
|
2022-04-19, Version 18.0.0 (Current)
Notable Changes:
Deprecations and Removals:
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) stream: remove thenable support (Robert Nagy)
(https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
fetch (experimental):
An experimental fetch API is available on the global scope by default.
The implementation is based upon https://undici.nodejs.org/#/,
an HTTP/1.1 client written for Node.js by contributors to the project.
Through this addition, the following globals are made available: `fetch`
, `FormData`, `Headers`, `Request`, `Response`.
Disable this API with the `--no-experimental-fetch` command-line flag.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/41811.
HTTP Timeouts:
`server.headersTimeout`, which limits the amount of time the parser will
wait to receive the complete HTTP headers, is now set to `60000` (60
seconds) by default.
`server.requestTimeout`, which sets the timeout value in milliseconds
for receiving the entire request from the client, is now set to `300000`
(5 minutes) by default.
If these timeouts expire, the server responds with status 408 without
forwarding the request to the request listener and then closes the
connection.
Both timeouts must be set to a non-zero value to protect against
potential Denial-of-Service attacks in case the server is deployed
without a reverse proxy in front.
Contributed by Paolo Insogna in https://github.com/nodejs/node/pull/41263.
Test Runner module (experimental):
The `node:test` module facilitates the creation of JavaScript tests that
report results in TAP format. This module is only available under the
`node:` scheme.
Contributed by Colin Ihrig in https://github.com/nodejs/node/pull/42325.
Toolchain and Compiler Upgrades:
- Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux
(RHEL) 8 and are compatible with Linux distributions based on glibc
2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
- Prebuilt binaries for macOS now require macOS 10.15 or later.
- For AIX the minimum supported architecture has been raised from Power
7 to Power 8.
Prebuilt binaries for 32-bit Windows will initially not be available due
to issues building the V8 dependency in Node.js. We hope to restore
32-bit Windows binaries for Node.js 18 with a future V8 update.
Node.js does not support running on operating systems that are no longer
supported by their vendor. For operating systems where their vendor has
planned to end support earlier than April 2025, such as Windows 8.1
(January 2023) and Windows Server 2012 R2 (October 2023), support for
Node.js 18 will end at the earlier date.
Full details about the supported toolchains and compilers are documented
in the Node.js `BUILDING.md` file.
Contributed by Richard Lau in https://github.com/nodejs/node/pull/42292,
https://github.com/nodejs/node/pull/42604 and https://github.com/nodejs/node/pull/42659
, and Michaël Zasso in https://github.com/nodejs/node/pull/42105 and
https://github.com/nodejs/node/pull/42666.
V8 10.1:
The V8 engine is updated to version 10.1, which is part of Chromium 101.
Compared to the version included in Node.js 17.9.0, the following new
features are included:
- The `findLast` and `findLastIndex` array methods.
- Improvements to the `Intl.Locale` API.
- The `Intl.supportedValuesOf` function.
- Improved performance of class fields and private class methods (the
initialization of them is now as fast as ordinary property stores).
The data format returned by the serialization API (`v8.serialize(value)`)
has changed, and cannot be deserialized by earlier versions of Node.js.
On the other hand, it is still possible to deserialize the previous
format, as the API is backwards-compatible.
Contributed by Michaël Zasso in https://github.com/nodejs/node/pull/42657.
Web Streams API (experimental):
Node.js now exposes the experimental implementation of the Web Streams
API on the global scope. This means the following APIs are now globally
available:
- `ReadableStream`, `ReadableStreamDefaultReader`,
`ReadableStreamBYOBReader`, `ReadableStreamBYOBRequest`,
`ReadableByteStreamController`, `ReadableStreamDefaultController`,
`TransformStream`, `TransformStreamDefaultController`, `WritableStream`,
`WritableStreamDefaultWriter`, `WritableStreamDefaultController`,
`ByteLengthQueuingStrategy`, `CountQueuingStrategy`, `TextEncoderStream`,
`TextDecoderStream`, `CompressionStream`, `DecompressionStream`.
Contributed James Snell in https://github.com/nodejs/node/pull/39062,
and Antoine du Hamel in https://github.com/nodejs/node/pull/42225.
Other Notable Changes:
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- doc: add RafaelGSS to collaborators
(RafaelGSS) (https://github.com/nodejs/node/pull/42718)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
Semver-Major Commits:
- (SEMVER-MAJOR) assert,util: compare RegExp.lastIndex while using deep
equal checks
(Ruben Bridgewater) (https://github.com/nodejs/node/pull/41020)
- (SEMVER-MAJOR) buffer: refactor `byteLength` to remove outdated
optimizations
(Rongjian Zhang) (https://github.com/nodejs/node/pull/38545)
- (SEMVER-MAJOR) buffer: expose Blob as a global
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) buffer: graduate Blob from experimental
(James M Snell) (https://github.com/nodejs/node/pull/41270)
- (SEMVER-MAJOR) build: make x86 Windows support temporarily
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42666)
- (SEMVER-MAJOR) build: bump macOS deployment target to 10.15
(Richard Lau) (https://github.com/nodejs/node/pull/42292)
- (SEMVER-MAJOR) build: downgrade Windows 8.1 and server 2012 R2 to
experimental
(Michaël Zasso) (https://github.com/nodejs/node/pull/42105)
- (SEMVER-MAJOR) child\_process: improve argument validation
(Rich Trott) (https://github.com/nodejs/node/pull/41305)
- (SEMVER-MAJOR) cluster: make `kill` to be just `process.kill`
(Bar Admoni) (https://github.com/nodejs/node/pull/34312)
- (SEMVER-MAJOR) crypto: cleanup validation
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/39841)
- (SEMVER-MAJOR) crypto: prettify othername in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42123)
- (SEMVER-MAJOR) crypto: fix X509Certificate toLegacyObject
(Tobias Nießen) (https://github.com/nodejs/node/pull/42124)
- (SEMVER-MAJOR) crypto: use RFC2253 format in PrintGeneralName
(Tobias Nießen) (https://github.com/nodejs/node/pull/42002)
- (SEMVER-MAJOR) crypto: change default check(Host|Email) behavior
(Tobias Nießen) (https://github.com/nodejs/node/pull/41600)
- (SEMVER-MAJOR) deps: V8: cherry-pick semver-major commits from 10.2
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 10.1.124.6
(Michaël Zasso) (https://github.com/nodejs/node/pull/42657)
- (SEMVER-MAJOR) deps: update V8 to 9.8.177.9
(Michaël Zasso) (https://github.com/nodejs/node/pull/41610)
- (SEMVER-MAJOR) deps: update V8 to 9.7.106.18
(Michaël Zasso) (https://github.com/nodejs/node/pull/40907)
- (SEMVER-MAJOR) dns: remove `dns.lookup` and `dnsPromises.lookup`
options type coercion
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) doc: update minimum glibc requirements for Linux
(Richard Lau) (https://github.com/nodejs/node/pull/42659)
- (SEMVER-MAJOR) doc: update AIX minimum supported arch
(Richard Lau) (https://github.com/nodejs/node/pull/42604)
- (SEMVER-MAJOR) fs: runtime deprecate string coercion in `fs.write`,
`fs.writeFileSync`
(Livia Medeiros) (https://github.com/nodejs/node/pull/42607)
- (SEMVER-MAJOR) http: refactor headersTimeout and requestTimeout logic
(Paolo Insogna) (https://github.com/nodejs/node/pull/41263)
- (SEMVER-MAJOR) http: make TCP noDelay enabled by default
(Paolo Insogna) (https://github.com/nodejs/node/pull/42163)
- (SEMVER-MAJOR) lib: enable fetch by default
(Michaël Zasso) (https://github.com/nodejs/node/pull/41811)
- (SEMVER-MAJOR) lib: replace validator and error
(Mohammed Keyvanzadeh) (https://github.com/nodejs/node/pull/41678)
- (SEMVER-MAJOR) module,repl: support 'node:'-only core modules
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) net: make `server.address()` return an integer for
`family`
(Antoine du Hamel) (https://github.com/nodejs/node/pull/41431)
- (SEMVER-MAJOR) process: disallow some uses of Object.defineProperty()
on process.env
(Himself65) (https://github.com/nodejs/node/pull/28006)
- (SEMVER-MAJOR) process: runtime deprecate multipleResolves
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41896)
- (SEMVER-MAJOR) readline: fix question still called after closed
(Xuguang Mei) (https://github.com/nodejs/node/pull/42464)
- (SEMVER-MAJOR) stream: remove thenable support
(Robert Nagy) (https://github.com/nodejs/node/pull/40773)
- (SEMVER-MAJOR) stream: expose web streams globals, remove runtime
experimental warning
(Antoine du Hamel) (https://github.com/nodejs/node/pull/42225)
- (SEMVER-MAJOR) stream: need to cleanup event listeners if last stream
is readable
(Xuguang Mei) (https://github.com/nodejs/node/pull/41954)
- (SEMVER-MAJOR) stream: revert revert `map` spec compliance
(Benjamin Gruenbaum) (https://github.com/nodejs/node/pull/41933)
- (SEMVER-MAJOR) stream: throw invalid arg type from End Of Stream
(Jithil P Ponnan) (https://github.com/nodejs/node/pull/41766)
- (SEMVER-MAJOR) stream: don't emit finish after destroy
(Robert Nagy) (https://github.com/nodejs/node/pull/40852)
- (SEMVER-MAJOR) stream: add errored and closed props
(Robert Nagy) (https://github.com/nodejs/node/pull/40696)
- (SEMVER-MAJOR) test: add initial test module
(Colin Ihrig) (https://github.com/nodejs/node/pull/42325)
- (SEMVER-MAJOR) timers: refactor internal classes to ES2015 syntax
(Rabbit) (https://github.com/nodejs/node/pull/37408)
- (SEMVER-MAJOR) tls: represent registeredID numerically always
(Tobias Nießen) (https://github.com/nodejs/node/pull/41561)
- (SEMVER-MAJOR) tls: move tls.parseCertString to end-of-life
(Tobias Nießen) (https://github.com/nodejs/node/pull/41479)
- (SEMVER-MAJOR) url: throw on NULL in IPv6 hostname
(Rich Trott) (https://github.com/nodejs/node/pull/42313)
- (SEMVER-MAJOR) v8: make v8.writeHeapSnapshot() error codes consistent
(Darshan Sen) (https://github.com/nodejs/node/pull/42577)
- (SEMVER-MAJOR) v8: make writeHeapSnapshot throw if fopen fails
(Antonio Román) (https://github.com/nodejs/node/pull/41373)
- (SEMVER-MAJOR) worker: expose BroadcastChannel as a global
(James M Snell) (https://github.com/nodejs/node/pull/41271)
- (SEMVER-MAJOR) worker: graduate BroadcastChannel to supported
(James M Snell) (https://github.com/nodejs/node/pull/41271)
PR-URL: https://github.com/nodejs/node/pull/42262
2022-03-08 01:39:47 +00:00
|
|
|
|
- version: v18.0.0
|
2022-03-09 15:37:49 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/42163
|
|
|
|
|
description: The `noDelay` option now defaults to `true`.
|
2022-04-23 21:03:46 -04:00
|
|
|
|
- version:
|
|
|
|
|
- v17.7.0
|
|
|
|
|
- v16.15.0
|
2022-03-09 15:37:49 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/41310
|
|
|
|
|
description: The `noDelay`, `keepAlive` and `keepAliveInitialDelay`
|
|
|
|
|
options are supported now.
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.8.0
|
|
|
|
|
- v12.15.0
|
|
|
|
|
- v10.19.0
|
2020-01-21 22:35:27 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31448
|
|
|
|
|
description: The `insecureHTTPParser` option is supported now.
|
2019-12-03 11:41:12 +01:00
|
|
|
|
- version: v13.3.0
|
2019-11-21 00:00:43 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30570
|
|
|
|
|
description: The `maxHeaderSize` option is supported now.
|
2020-10-01 20:23:33 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v9.6.0
|
|
|
|
|
- v8.12.0
|
2017-10-19 20:16:02 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/15752
|
|
|
|
|
description: The `options` argument is supported now.
|
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
|
* `options` {Object}
|
2022-12-01 18:37:50 +01:00
|
|
|
|
* `connectionsCheckingInterval`: Sets the interval value in milliseconds to
|
|
|
|
|
check for request and headers timeout in incomplete requests.
|
|
|
|
|
**Default:** `30000`.
|
2022-04-13 16:47:59 +02:00
|
|
|
|
* `headersTimeout`: Sets the timeout value in milliseconds for receiving
|
|
|
|
|
the complete HTTP headers from the client.
|
|
|
|
|
See [`server.headersTimeout`][] for more information.
|
|
|
|
|
**Default:** `60000`.
|
2023-04-24 13:35:38 +08:00
|
|
|
|
* `highWaterMark` {number} Optionally overrides all `socket`s'
|
|
|
|
|
`readableHighWaterMark` and `writableHighWaterMark`. This affects
|
|
|
|
|
`highWaterMark` property of both `IncomingMessage` and `ServerResponse`.
|
|
|
|
|
**Default:** See [`stream.getDefaultHighWaterMark()`][].
|
2023-09-16 13:08:18 +02:00
|
|
|
|
* `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.
|
2022-12-01 18:37:50 +01:00
|
|
|
|
**Default:** `false`.
|
|
|
|
|
* `IncomingMessage` {http.IncomingMessage} Specifies the `IncomingMessage`
|
|
|
|
|
class to be used. Useful for extending the original `IncomingMessage`.
|
|
|
|
|
**Default:** `IncomingMessage`.
|
2023-07-28 12:47:29 +02:00
|
|
|
|
* `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`][].
|
2023-07-03 08:20:24 +02:00
|
|
|
|
**Default:** `false`.
|
2022-12-01 18:37:50 +01:00
|
|
|
|
* `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`.
|
2022-04-13 16:47:59 +02:00
|
|
|
|
* `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`.
|
2019-11-21 00:00:43 +01:00
|
|
|
|
* `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.
|
2022-04-20 00:46:37 +02:00
|
|
|
|
**Default:** 16384 (16 KiB).
|
2022-02-22 11:58:30 +01:00
|
|
|
|
* `noDelay` {boolean} If set to `true`, it disables the use of Nagle's
|
|
|
|
|
algorithm immediately after a new incoming connection is received.
|
2022-03-09 15:37:49 +01:00
|
|
|
|
**Default:** `true`.
|
2022-12-01 18:37:50 +01:00
|
|
|
|
* `requestTimeout`: Sets the timeout value in milliseconds for receiving
|
|
|
|
|
the entire request from the client.
|
|
|
|
|
See [`server.requestTimeout`][] for more information.
|
2023-02-03 23:51:55 +09:00
|
|
|
|
**Default:** `300000`.
|
2023-07-25 09:19:44 +02:00
|
|
|
|
* `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).
|
2021-07-09 15:59:35 +08:00
|
|
|
|
**Default:** `true`.
|
2022-12-01 18:37:50 +01:00
|
|
|
|
* `ServerResponse` {http.ServerResponse} Specifies the `ServerResponse` class
|
|
|
|
|
to be used. Useful for extending the original `ServerResponse`. **Default:**
|
|
|
|
|
`ServerResponse`.
|
|
|
|
|
* `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 `; `.
|
2024-06-12 02:09:17 +09:30
|
|
|
|
* `rejectNonStandardBodyWrites` {boolean} If set to `true`, an error is thrown
|
|
|
|
|
when writing to an HTTP response which does not have a body.
|
|
|
|
|
**Default:** `false`.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
|
* `requestListener` {Function}
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* Returns: {http.Server}
|
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
Returns a new instance of [`http.Server`][].
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
|
|
|
|
The `requestListener` is a function which is automatically
|
2016-09-10 16:03:30 -07:00
|
|
|
|
added to the [`'request'`][] event.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
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);
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-19 16:44:29 -05:00
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2021-07-19 16:44:29 -05:00
|
|
|
|
|
|
|
|
|
// 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({
|
2022-11-17 08:19:12 -05:00
|
|
|
|
data: 'Hello World!',
|
2021-07-19 16:44:29 -05:00
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
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);
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-19 16:44:29 -05:00
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2021-07-19 16:44:29 -05:00
|
|
|
|
|
|
|
|
|
// 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({
|
2022-11-17 08:19:12 -05:00
|
|
|
|
data: 'Hello World!',
|
2021-07-19 16:44:29 -05:00
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.get(options[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.get(url[, options][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
2017-06-02 18:07:06 +03:00
|
|
|
|
changes:
|
2018-08-13 19:02:06 +10:00
|
|
|
|
- version: v10.9.0
|
2018-07-01 11:00:24 -04:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21616
|
2018-09-19 17:14:37 +02:00
|
|
|
|
description: The `url` parameter can now be passed along with a separate
|
|
|
|
|
`options` object.
|
2017-06-02 18:07:06 +03:00
|
|
|
|
- version: v7.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10638
|
|
|
|
|
description: The `options` parameter can be a WHATWG `URL` object.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2018-07-01 11:00:24 -04:00
|
|
|
|
* `url` {string | URL}
|
|
|
|
|
* `options` {Object} Accepts the same `options` as
|
2023-07-08 17:25:27 +08:00
|
|
|
|
[`http.request()`][], with the method set to GET by default.
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* Returns: {http.ClientRequest}
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Since most requests are GET requests without bodies, Node.js provides this
|
2016-10-12 21:57:04 +02:00
|
|
|
|
convenience method. The only difference between this method and
|
2023-07-08 17:25:27 +08:00
|
|
|
|
[`http.request()`][] is that it sets the method to GET by default and calls `req.end()`
|
2019-06-20 13:52:54 -06:00
|
|
|
|
automatically. The callback must take care to consume the response
|
2017-08-26 18:20:34 -04:00
|
|
|
|
data for reasons stated in [`http.ClientRequest`][] section.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2016-10-12 21:57:04 +02:00
|
|
|
|
The `callback` is invoked with a single argument that is an instance of
|
2018-04-29 14:16:44 +03:00
|
|
|
|
[`http.IncomingMessage`][].
|
2016-10-12 21:57:04 +02:00
|
|
|
|
|
2018-08-26 19:02:27 +03:00
|
|
|
|
JSON fetching example:
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2021-04-02 11:16:09 +05:00
|
|
|
|
http.get('http://localhost:8000/', (res) => {
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const { statusCode } = res;
|
2016-10-12 21:57:04 +02:00
|
|
|
|
const contentType = res.headers['content-type'];
|
|
|
|
|
|
|
|
|
|
let error;
|
2020-07-06 12:33:10 -07:00
|
|
|
|
// Any 2xx status code signals a successful response but
|
|
|
|
|
// here we're only checking for 200.
|
2016-10-12 21:57:04 +02:00
|
|
|
|
if (statusCode !== 200) {
|
2017-04-21 17:38:31 +03:00
|
|
|
|
error = new Error('Request Failed.\n' +
|
2016-10-12 21:57:04 +02:00
|
|
|
|
`Status Code: ${statusCode}`);
|
|
|
|
|
} else if (!/^application\/json/.test(contentType)) {
|
2017-04-21 17:38:31 +03:00
|
|
|
|
error = new Error('Invalid content-type.\n' +
|
2016-10-12 21:57:04 +02:00
|
|
|
|
`Expected application/json but received ${contentType}`);
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
2017-04-02 21:39:42 +03:00
|
|
|
|
console.error(error.message);
|
2019-03-07 01:03:53 +01:00
|
|
|
|
// Consume response data to free up memory
|
2016-10-12 21:57:04 +02:00
|
|
|
|
res.resume();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
|
let rawData = '';
|
2017-04-02 21:39:42 +03:00
|
|
|
|
res.on('data', (chunk) => { rawData += chunk; });
|
2016-10-12 21:57:04 +02:00
|
|
|
|
res.on('end', () => {
|
|
|
|
|
try {
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const parsedData = JSON.parse(rawData);
|
2016-10-12 21:57:04 +02:00
|
|
|
|
console.log(parsedData);
|
|
|
|
|
} catch (e) {
|
2017-04-02 21:39:42 +03:00
|
|
|
|
console.error(e.message);
|
2016-10-12 21:57:04 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
}).on('error', (e) => {
|
2017-04-02 21:39:42 +03:00
|
|
|
|
console.error(`Got error: ${e.message}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
2021-04-02 11:16:09 +05:00
|
|
|
|
|
|
|
|
|
// 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({
|
2022-11-17 08:19:12 -05:00
|
|
|
|
data: 'Hello World!',
|
2021-04-02 11:16:09 +05:00
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.globalAgent`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
2022-06-21 14:50:55 +02:00
|
|
|
|
changes:
|
|
|
|
|
- version:
|
2022-09-13 12:53:52 -03:00
|
|
|
|
- v19.0.0
|
2022-06-21 14:50:55 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/43522
|
2024-04-08 22:33:46 +02:00
|
|
|
|
description: The agent now uses HTTP Keep-Alive and a 5 second timeout by
|
|
|
|
|
default.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* {http.Agent}
|
|
|
|
|
|
2017-01-09 12:45:46 -05:00
|
|
|
|
Global instance of `Agent` which is used as the default for all HTTP client
|
2024-04-08 22:33:46 +02:00
|
|
|
|
requests. Diverges from a default `Agent` configuration by having `keepAlive`
|
|
|
|
|
enabled and a `timeout` of 5 seconds.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.maxHeaderSize`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2018-12-05 19:59:12 -05:00
|
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
|
added:
|
|
|
|
|
- v11.6.0
|
|
|
|
|
- v10.15.0
|
2018-12-05 19:59:12 -05:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* {number}
|
|
|
|
|
|
|
|
|
|
Read-only property specifying the maximum allowed size of HTTP headers in bytes.
|
2022-04-20 00:46:37 +02:00
|
|
|
|
Defaults to 16 KiB. Configurable using the [`--max-http-header-size`][] CLI
|
2021-07-10 10:33:54 -07:00
|
|
|
|
option.
|
2018-12-05 19:59:12 -05:00
|
|
|
|
|
2019-11-21 00:00:43 +01:00
|
|
|
|
This can be overridden for servers and client requests by passing the
|
|
|
|
|
`maxHeaderSize` option.
|
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.request(options[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2019-12-24 03:53:13 -08:00
|
|
|
|
## `http.request(url[, options][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2016-06-23 21:50:36 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
2017-06-02 18:07:06 +03:00
|
|
|
|
changes:
|
2021-09-04 15:29:35 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v16.7.0
|
|
|
|
|
- v14.18.0
|
2021-07-08 11:43:26 -04:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/39310
|
|
|
|
|
description: When using a `URL` object parsed username and
|
|
|
|
|
password will now be properly URI decoded.
|
2021-05-11, Version 14.17.0 'Fermium' (LTS)
Notable Changes:
Diagnostics channel (experimental module):
`diagnostics_channel` is a new experimental module that provides an API
to create named channels to report arbitrary message data for
diagnostics purposes.
The module was initially introduced in Node.js v15.1.0 and is
backported to v14.17.0 to enable testing it at a larger scale.
With `diagnostics_channel`, Node.js core and module authors can publish
contextual data about what they are doing at a given time. This could
be the hostname and query string of a mysql query, for example. Just
create a named channel with `dc.channel(name)` and call
`channel.publish(data)` to send the data to any listeners to that
channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
MySQL.prototype.query = function query(queryString, values, callback) {
// Broadcast query information whenever a query is made
channel.publish({
query: queryString,
host: this.hostname,
});
this.doQuery(queryString, values, callback);
};
```
Channels are like one big global event emitter but are split into
separate objects to ensure they get the best performance. If nothing is
listening to the channel, the publishing overhead should be as close to
zero as possible. Consuming channel data is as easy as using
`channel.subscribe(listener)` to run a function whenever a message is
published to that channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
channel.subscribe(({ query, host }) => {
console.log(`mysql query to ${host}: ${query}`);
});
```
The data captured can be used to provide context for what an app is
doing at a given time. This can be used for things like augmenting
tracing data, tracking network and filesystem activity, logging
queries, and many other things. It's also a very useful data source
for diagnostics tools to provide a clearer picture of exactly what the
application is doing at a given point in the data they are presenting.
Contributed by Stephen Belanger (https://github.com/nodejs/node/pull/34895).
UUID support in the crypto module:
The new `crypto.randomUUID()` method now allows to generate random
[RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4
UUID strings:
```js
const { randomUUID } = require('crypto');
console.log(randomUUID());
// 'aa7c91a1-f8fc-4339-b9db-f93fc7233429'
```
Contributed by James M Snell (https://github.com/nodejs/node/pull/36729).
Experimental support for `AbortController` and `AbortSignal`:
Node.js 14.17.0 adds experimental partial support for `AbortController`
and `AbortSignal`.
Both constructors can be enabled globally using the
`--experimental-abortcontroller` flag.
Additionally, several Node.js APIs have been updated to support
`AbortSignal` for cancellation.
It is not mandatory to use the built-in constructors with them. Any
spec-compliant third-party alternatives should be compatible.
`AbortSignal` support was added to the following methods:
* `child_process.exec`
* `child_process.execFile`
* `child_process.fork`
* `child_process.spawn`
* `dgram.createSocket`
* `events.on`
* `events.once`
* `fs.readFile`
* `fs.watch`
* `fs.writeFile`
* `http.request`
* `https.request`
* `http2Session.request`
* The promisified variants of `setImmediate` and `setTimeout`
Other notable changes:
* doc:
* revoke deprecation of legacy url, change status to legacy (James M Snell) (https://github.com/nodejs/node/pull/37784)
* add legacy status to stability index (James M Snell) (https://github.com/nodejs/node/pull/37784)
* upgrade stability status of report API (Gireesh Punathil) (https://github.com/nodejs/node/pull/35654)
* deps:
* V8: Backport various patches for Apple Silicon support (BoHong Li) (https://github.com/nodejs/node/pull/38051)
* update ICU to 68.1 (Michaël Zasso) (https://github.com/nodejs/node/pull/36187)
* upgrade to libuv 1.41.0 (Colin Ihrig) (https://github.com/nodejs/node/pull/37360)
* http:
* add http.ClientRequest.getRawHeaderNames() (simov) (https://github.com/nodejs/node/pull/37660)
* report request start and end with diagnostics\_channel (Stephen Belanger) (https://github.com/nodejs/node/pull/34895)
* util:
* add getSystemErrorMap() impl (eladkeyshawn) (https://github.com/nodejs/node/pull/38101)
PR-URL: https://github.com/nodejs/node/pull/38507
2021-05-02 23:12:18 -04:00
|
|
|
|
- version:
|
|
|
|
|
- v15.3.0
|
|
|
|
|
- v14.17.0
|
2020-11-09 12:51:14 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/36048
|
|
|
|
|
description: It is possible to abort a request with an AbortSignal.
|
2020-04-24 18:43:06 +02:00
|
|
|
|
- version:
|
|
|
|
|
- v13.8.0
|
|
|
|
|
- v12.15.0
|
|
|
|
|
- v10.19.0
|
2020-01-21 22:35:27 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/31448
|
|
|
|
|
description: The `insecureHTTPParser` option is supported now.
|
2019-12-03 11:41:12 +01:00
|
|
|
|
- version: v13.3.0
|
2019-11-21 00:00:43 +01:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/30570
|
|
|
|
|
description: The `maxHeaderSize` option is supported now.
|
2018-08-13 19:02:06 +10:00
|
|
|
|
- version: v10.9.0
|
2018-07-01 11:00:24 -04:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/21616
|
2018-09-19 17:14:37 +02:00
|
|
|
|
description: The `url` parameter can now be passed along with a separate
|
|
|
|
|
`options` object.
|
2017-06-02 18:07:06 +03:00
|
|
|
|
- version: v7.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10638
|
|
|
|
|
description: The `options` parameter can be a WHATWG `URL` object.
|
2016-06-23 21:50:36 +02:00
|
|
|
|
-->
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2018-07-01 11:00:24 -04:00
|
|
|
|
* `url` {string | URL}
|
|
|
|
|
* `options` {Object}
|
2019-02-16 19:47:27 +01:00
|
|
|
|
* `agent` {http.Agent | boolean} Controls [`Agent`][] behavior. Possible
|
|
|
|
|
values:
|
|
|
|
|
* `undefined` (default): use [`http.globalAgent`][] for this host and port.
|
|
|
|
|
* `Agent` object: explicitly use the passed in `Agent`.
|
|
|
|
|
* `false`: causes a new `Agent` with default values to be used.
|
2022-01-07 21:31:40 -08:00
|
|
|
|
* `auth` {string} Basic authentication (`'user:password'`) to compute an
|
2019-02-16 19:47:27 +01:00
|
|
|
|
Authorization header.
|
|
|
|
|
* `createConnection` {Function} A function that produces a socket/stream to
|
|
|
|
|
use for the request when the `agent` option is not used. This can be used to
|
|
|
|
|
avoid creating a custom `Agent` class just to override the default
|
|
|
|
|
`createConnection` function. See [`agent.createConnection()`][] for more
|
|
|
|
|
details. Any [`Duplex`][] stream is a valid return value.
|
|
|
|
|
* `defaultPort` {number} Default port for the protocol. **Default:**
|
|
|
|
|
`agent.defaultPort` if an `Agent` is used, else `undefined`.
|
|
|
|
|
* `family` {number} IP address family to use when resolving `host` or
|
|
|
|
|
`hostname`. Valid values are `4` or `6`. When unspecified, both IP v4 and
|
|
|
|
|
v6 will be used.
|
|
|
|
|
* `headers` {Object} An object containing request headers.
|
2021-03-13 20:54:55 +01:00
|
|
|
|
* `hints` {number} Optional [`dns.lookup()` hints][].
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `host` {string} A domain name or IP address of the server to issue the
|
2018-04-09 14:22:14 +03:00
|
|
|
|
request to. **Default:** `'localhost'`.
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `hostname` {string} Alias for `host`. To support [`url.parse()`][],
|
2018-11-06 09:58:42 -08:00
|
|
|
|
`hostname` will be used if both `host` and `hostname` are specified.
|
2023-09-16 13:08:18 +02:00
|
|
|
|
* `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.
|
2020-01-21 22:35:27 +01:00
|
|
|
|
**Default:** `false`
|
2023-07-03 08:20:24 +02:00
|
|
|
|
* `joinDuplicateHeaders` {boolean} It joins the field line values of
|
|
|
|
|
multiple headers in a request with `, ` instead of discarding
|
|
|
|
|
the duplicates. See [`message.headers`][] for more information.
|
|
|
|
|
**Default:** `false`.
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `localAddress` {string} Local interface to bind for network connections.
|
2021-03-03 16:35:31 +01:00
|
|
|
|
* `localPort` {number} Local port to connect from.
|
2019-11-10 18:52:18 +01:00
|
|
|
|
* `lookup` {Function} Custom lookup function. **Default:** [`dns.lookup()`][].
|
2019-11-21 00:00:43 +01:00
|
|
|
|
* `maxHeaderSize` {number} Optionally overrides the value of
|
2022-01-07 21:31:40 -08:00
|
|
|
|
[`--max-http-header-size`][] (the maximum length of response headers in
|
|
|
|
|
bytes) for responses received from the server.
|
2022-04-20 00:46:37 +02:00
|
|
|
|
**Default:** 16384 (16 KiB).
|
2018-04-02 04:44:32 +03:00
|
|
|
|
* `method` {string} A string specifying the HTTP request method. **Default:**
|
2016-11-13 06:43:55 -08:00
|
|
|
|
`'GET'`.
|
2018-04-02 04:44:32 +03:00
|
|
|
|
* `path` {string} Request path. Should include query string if any.
|
|
|
|
|
E.G. `'/index.html?page=12'`. An exception is thrown when the request path
|
|
|
|
|
contains illegal characters. Currently, only spaces are rejected but that
|
|
|
|
|
may change in the future. **Default:** `'/'`.
|
2019-02-16 19:47:27 +01:00
|
|
|
|
* `port` {number} Port of remote server. **Default:** `defaultPort` if set,
|
|
|
|
|
else `80`.
|
|
|
|
|
* `protocol` {string} Protocol to use. **Default:** `'http:'`.
|
2024-12-12 16:43:10 +00:00
|
|
|
|
* `setDefaultHeaders` {boolean}: Specifies whether or not to automatically add
|
|
|
|
|
default headers such as `Connection`, `Content-Length`, `Transfer-Encoding`,
|
|
|
|
|
and `Host`. If set to `false` then all necessary headers must be added
|
|
|
|
|
manually. Defaults to `true`.
|
2018-03-21 12:11:40 +08:00
|
|
|
|
* `setHost` {boolean}: Specifies whether or not to automatically add the
|
2024-12-12 16:43:10 +00:00
|
|
|
|
`Host` header. If provided, this overrides `setDefaultHeaders`. Defaults to
|
|
|
|
|
`true`.
|
2022-01-10 21:50:58 +01:00
|
|
|
|
* `signal` {AbortSignal}: An AbortSignal that may be used to abort an ongoing
|
|
|
|
|
request.
|
2022-01-07 21:31:40 -08:00
|
|
|
|
* `socketPath` {string} Unix domain socket. Cannot be used if one of `host`
|
|
|
|
|
or `port` is specified, as those specify a TCP Socket.
|
2019-02-16 19:47:27 +01:00
|
|
|
|
* `timeout` {number}: A number specifying the socket timeout in milliseconds.
|
|
|
|
|
This will set the timeout before the socket is connected.
|
2022-01-10 21:50:58 +01:00
|
|
|
|
* `uniqueHeaders` {Array} A list of request headers that should be sent
|
|
|
|
|
only once. If the header's value is an array, the items will be joined
|
|
|
|
|
using `; `.
|
2016-09-10 16:03:30 -07:00
|
|
|
|
* `callback` {Function}
|
|
|
|
|
* Returns: {http.ClientRequest}
|
|
|
|
|
|
2022-02-22 11:58:30 +01:00
|
|
|
|
`options` in [`socket.connect()`][] are also supported.
|
|
|
|
|
|
2015-11-04 17:56:03 -05:00
|
|
|
|
Node.js maintains several connections per server to make HTTP requests.
|
|
|
|
|
This function allows one to transparently issue requests.
|
|
|
|
|
|
2018-07-01 11:00:24 -04:00
|
|
|
|
`url` can be a string or a [`URL`][] object. If `url` is a
|
2018-04-24 19:37:43 -05:00
|
|
|
|
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
|
2017-06-02 18:07:06 +03:00
|
|
|
|
object, it will be automatically converted to an ordinary `options` object.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2018-07-01 11:00:24 -04:00
|
|
|
|
If both `url` and `options` are specified, the objects are merged, with the
|
|
|
|
|
`options` properties taking precedence.
|
|
|
|
|
|
2017-12-06 10:15:58 -08:00
|
|
|
|
The optional `callback` parameter will be added as a one-time listener for
|
2016-07-09 08:13:09 +03:00
|
|
|
|
the [`'response'`][] event.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
|
`http.request()` returns an instance of the [`http.ClientRequest`][]
|
2015-11-04 17:56:03 -05:00
|
|
|
|
class. The `ClientRequest` instance is a writable stream. If one needs to
|
|
|
|
|
upload a file with a POST request, then write to the `ClientRequest` object.
|
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import http from 'node:http';
|
|
|
|
|
import { Buffer } from 'node:buffer';
|
|
|
|
|
|
|
|
|
|
const postData = JSON.stringify({
|
|
|
|
|
'msg': 'Hello World!',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
hostname: 'www.google.com',
|
|
|
|
|
port: 80,
|
|
|
|
|
path: '/upload',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Content-Length': Buffer.byteLength(postData),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const req = http.request(options, (res) => {
|
|
|
|
|
console.log(`STATUS: ${res.statusCode}`);
|
|
|
|
|
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
|
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
|
res.on('data', (chunk) => {
|
|
|
|
|
console.log(`BODY: ${chunk}`);
|
|
|
|
|
});
|
|
|
|
|
res.on('end', () => {
|
|
|
|
|
console.log('No more data in response.');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
|
console.error(`problem with request: ${e.message}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Write data to request body
|
|
|
|
|
req.write(postData);
|
|
|
|
|
req.end();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const http = require('node:http');
|
2021-05-22 22:38:58 +03:00
|
|
|
|
|
|
|
|
|
const postData = JSON.stringify({
|
2022-11-17 08:19:12 -05:00
|
|
|
|
'msg': 'Hello World!',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
hostname: 'www.google.com',
|
|
|
|
|
port: 80,
|
|
|
|
|
path: '/upload',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
2021-05-22 22:38:58 +03:00
|
|
|
|
'Content-Type': 'application/json',
|
2022-11-17 08:19:12 -05:00
|
|
|
|
'Content-Length': Buffer.byteLength(postData),
|
|
|
|
|
},
|
2016-01-17 18:39:07 +01:00
|
|
|
|
};
|
|
|
|
|
|
2017-04-02 21:39:42 +03:00
|
|
|
|
const req = http.request(options, (res) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
console.log(`STATUS: ${res.statusCode}`);
|
|
|
|
|
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
|
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
|
res.on('data', (chunk) => {
|
|
|
|
|
console.log(`BODY: ${chunk}`);
|
|
|
|
|
});
|
|
|
|
|
res.on('end', () => {
|
2016-07-14 22:41:29 -07:00
|
|
|
|
console.log('No more data in response.');
|
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
2017-04-02 21:39:42 +03:00
|
|
|
|
console.error(`problem with request: ${e.message}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
|
// Write data to request body
|
2016-01-17 18:39:07 +01:00
|
|
|
|
req.write(postData);
|
|
|
|
|
req.end();
|
|
|
|
|
```
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
In the example `req.end()` was called. With `http.request()` one
|
2017-03-03 15:45:20 -08:00
|
|
|
|
must always call `req.end()` to signify the end of the request -
|
2015-11-04 17:56:03 -05:00
|
|
|
|
even if there is no data being written to the request body.
|
|
|
|
|
|
|
|
|
|
If any error is encountered during the request (be that with DNS resolution,
|
|
|
|
|
TCP level errors, or actual HTTP parse errors) an `'error'` event is emitted
|
2015-12-14 10:25:10 -08:00
|
|
|
|
on the returned request object. As with all `'error'` events, if no listeners
|
|
|
|
|
are registered the error will be thrown.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
|
|
|
|
There are a few special headers that should be noted.
|
|
|
|
|
|
|
|
|
|
* Sending a 'Connection: keep-alive' will notify Node.js that the connection to
|
|
|
|
|
the server should be persisted until the next request.
|
|
|
|
|
|
2017-01-24 21:11:17 +11:00
|
|
|
|
* Sending a 'Content-Length' header will disable the default chunked encoding.
|
2015-11-04 17:56:03 -05:00
|
|
|
|
|
|
|
|
|
* Sending an 'Expect' header will immediately send the request headers.
|
2017-03-03 15:45:20 -08:00
|
|
|
|
Usually, when sending 'Expect: 100-continue', both a timeout and a listener
|
2019-03-17 21:32:12 -07:00
|
|
|
|
for the `'continue'` event should be set. See RFC 2616 Section 8.2.3 for more
|
2015-11-04 17:56:03 -05:00
|
|
|
|
information.
|
|
|
|
|
|
|
|
|
|
* Sending an Authorization header will override using the `auth` option
|
|
|
|
|
to compute basic authentication.
|
2012-06-06 15:05:18 -04:00
|
|
|
|
|
2017-06-02 18:07:06 +03:00
|
|
|
|
Example using a [`URL`][] as `options`:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const options = new URL('http://abc:xyz@example.com');
|
|
|
|
|
|
|
|
|
|
const req = http.request(options, (res) => {
|
|
|
|
|
// ...
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-07 18:43:39 -06:00
|
|
|
|
In a successful request, the following events will be emitted in the following
|
|
|
|
|
order:
|
|
|
|
|
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'socket'`
|
|
|
|
|
* `'response'`
|
|
|
|
|
* `'data'` any number of times, on the `res` object
|
|
|
|
|
(`'data'` will not be emitted at all if the response body is empty, for
|
2018-01-07 18:43:39 -06:00
|
|
|
|
instance, in most redirects)
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'end'` on the `res` object
|
|
|
|
|
* `'close'`
|
2018-01-07 18:43:39 -06:00
|
|
|
|
|
|
|
|
|
In the case of a connection error, the following events will be emitted:
|
|
|
|
|
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'socket'`
|
|
|
|
|
* `'error'`
|
|
|
|
|
* `'close'`
|
2018-01-07 18:43:39 -06:00
|
|
|
|
|
2019-05-30 20:24:12 +02:00
|
|
|
|
In the case of a premature connection close before the response is received,
|
|
|
|
|
the following events will be emitted in the following order:
|
|
|
|
|
|
|
|
|
|
* `'socket'`
|
|
|
|
|
* `'error'` with an error with message `'Error: socket hang up'` and code
|
|
|
|
|
`'ECONNRESET'`
|
|
|
|
|
* `'close'`
|
|
|
|
|
|
|
|
|
|
In the case of a premature connection close after the response is received,
|
|
|
|
|
the following events will be emitted in the following order:
|
|
|
|
|
|
|
|
|
|
* `'socket'`
|
|
|
|
|
* `'response'`
|
|
|
|
|
* `'data'` any number of times, on the `res` object
|
|
|
|
|
* (connection closed here)
|
|
|
|
|
* `'aborted'` on the `res` object
|
2024-03-25 00:53:58 -07:00
|
|
|
|
* `'close'`
|
2020-04-30 20:29:35 +02:00
|
|
|
|
* `'error'` on the `res` object with an error with message
|
2023-03-26 13:30:05 -07:00
|
|
|
|
`'Error: aborted'` and code `'ECONNRESET'`
|
2019-05-30 20:24:12 +02:00
|
|
|
|
* `'close'` on the `res` object
|
|
|
|
|
|
2020-04-13 11:02:03 +02:00
|
|
|
|
If `req.destroy()` is called before a socket is assigned, the following
|
|
|
|
|
events will be emitted in the following order:
|
|
|
|
|
|
|
|
|
|
* (`req.destroy()` called here)
|
|
|
|
|
* `'error'` with an error with message `'Error: socket hang up'` and code
|
2023-03-26 13:30:05 -07:00
|
|
|
|
`'ECONNRESET'`, or the error with which `req.destroy()` was called
|
2020-04-13 11:02:03 +02:00
|
|
|
|
* `'close'`
|
|
|
|
|
|
|
|
|
|
If `req.destroy()` is called before the connection succeeds, the following
|
|
|
|
|
events will be emitted in the following order:
|
|
|
|
|
|
|
|
|
|
* `'socket'`
|
|
|
|
|
* (`req.destroy()` called here)
|
|
|
|
|
* `'error'` with an error with message `'Error: socket hang up'` and code
|
2023-03-26 13:30:05 -07:00
|
|
|
|
`'ECONNRESET'`, or the error with which `req.destroy()` was called
|
2020-04-13 11:02:03 +02:00
|
|
|
|
* `'close'`
|
|
|
|
|
|
|
|
|
|
If `req.destroy()` is called after the response is received, the following
|
|
|
|
|
events will be emitted in the following order:
|
|
|
|
|
|
|
|
|
|
* `'socket'`
|
|
|
|
|
* `'response'`
|
|
|
|
|
* `'data'` any number of times, on the `res` object
|
|
|
|
|
* (`req.destroy()` called here)
|
|
|
|
|
* `'aborted'` on the `res` object
|
2024-03-25 00:53:58 -07:00
|
|
|
|
* `'close'`
|
2023-03-26 13:30:05 -07:00
|
|
|
|
* `'error'` on the `res` object with an error with message `'Error: aborted'`
|
|
|
|
|
and code `'ECONNRESET'`, or the error with which `req.destroy()` was called
|
2020-04-13 11:02:03 +02:00
|
|
|
|
* `'close'` on the `res` object
|
|
|
|
|
|
|
|
|
|
If `req.abort()` is called before a socket is assigned, the following
|
|
|
|
|
events will be emitted in the following order:
|
|
|
|
|
|
|
|
|
|
* (`req.abort()` called here)
|
|
|
|
|
* `'abort'`
|
|
|
|
|
* `'close'`
|
|
|
|
|
|
|
|
|
|
If `req.abort()` is called before the connection succeeds, the following
|
|
|
|
|
events will be emitted in the following order:
|
2018-01-07 18:43:39 -06:00
|
|
|
|
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'socket'`
|
2018-01-07 18:43:39 -06:00
|
|
|
|
* (`req.abort()` called here)
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'abort'`
|
2019-07-15 07:57:40 -07:00
|
|
|
|
* `'error'` with an error with message `'Error: socket hang up'` and code
|
|
|
|
|
`'ECONNRESET'`
|
2018-12-02 09:24:47 +01:00
|
|
|
|
* `'close'`
|
2018-01-07 18:43:39 -06:00
|
|
|
|
|
2020-04-13 11:02:03 +02:00
|
|
|
|
If `req.abort()` is called after the response is received, the following
|
|
|
|
|
events will be emitted in the following order:
|
2018-01-07 18:43:39 -06:00
|
|
|
|
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'socket'`
|
|
|
|
|
* `'response'`
|
|
|
|
|
* `'data'` any number of times, on the `res` object
|
2018-01-07 18:43:39 -06:00
|
|
|
|
* (`req.abort()` called here)
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'abort'`
|
2018-12-02 09:24:47 +01:00
|
|
|
|
* `'aborted'` on the `res` object
|
2020-04-30 20:29:35 +02:00
|
|
|
|
* `'error'` on the `res` object with an error with message
|
|
|
|
|
`'Error: aborted'` and code `'ECONNRESET'`.
|
2018-04-09 19:30:22 +03:00
|
|
|
|
* `'close'`
|
2018-12-02 09:24:47 +01:00
|
|
|
|
* `'close'` on the `res` object
|
2018-04-09 19:30:22 +03:00
|
|
|
|
|
2019-06-20 13:52:54 -06:00
|
|
|
|
Setting the `timeout` option or using the `setTimeout()` function will
|
2018-04-09 19:30:22 +03:00
|
|
|
|
not abort the request or do anything besides add a `'timeout'` event.
|
2018-01-07 18:43:39 -06:00
|
|
|
|
|
2023-03-26 13:30:05 -07:00
|
|
|
|
Passing an `AbortSignal` and then calling `abort()` on the corresponding
|
2020-11-09 12:51:14 +02:00
|
|
|
|
`AbortController` will behave the same way as calling `.destroy()` on the
|
2023-03-26 13:30:05 -07:00
|
|
|
|
request. Specifically, the `'error'` event will be emitted with an error with
|
|
|
|
|
the message `'AbortError: The operation was aborted'`, the code `'ABORT_ERR'`
|
|
|
|
|
and the `cause`, if one was provided.
|
2020-11-09 12:51:14 +02:00
|
|
|
|
|
2023-01-17 22:40:39 +09:00
|
|
|
|
## `http.validateHeaderName(name[, label])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-04-28 12:30:20 +03:00
|
|
|
|
<!-- YAML
|
2020-05-17 18:45:37 -07:00
|
|
|
|
added: v14.3.0
|
2023-01-17 22:40:39 +09:00
|
|
|
|
changes:
|
2023-01-28 16:47:47 -05:00
|
|
|
|
- version:
|
|
|
|
|
- v19.5.0
|
|
|
|
|
- v18.14.0
|
2023-01-17 22:40:39 +09:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/46143
|
|
|
|
|
description: The `label` parameter is added.
|
2020-04-28 12:30:20 +03:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
2023-01-17 22:40:39 +09:00
|
|
|
|
* `label` {string} Label for error message. **Default:** `'Header name'`.
|
2020-04-28 12:30:20 +03:00
|
|
|
|
|
|
|
|
|
Performs the low-level validations on the provided `name` that are done when
|
|
|
|
|
`res.setHeader(name, value)` is called.
|
|
|
|
|
|
|
|
|
|
Passing illegal value as `name` will result in a [`TypeError`][] being thrown,
|
|
|
|
|
identified by `code: 'ERR_INVALID_HTTP_TOKEN'`.
|
|
|
|
|
|
|
|
|
|
It is not necessary to use this method before passing headers to an HTTP request
|
|
|
|
|
or response. The HTTP module will automatically validate such headers.
|
|
|
|
|
|
|
|
|
|
Example:
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import { validateHeaderName } from 'node:http';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
validateHeaderName('');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err instanceof TypeError); // --> true
|
|
|
|
|
console.error(err.code); // --> 'ERR_INVALID_HTTP_TOKEN'
|
|
|
|
|
console.error(err.message); // --> 'Header name must be a valid HTTP token [""]'
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const { validateHeaderName } = require('node:http');
|
2020-04-28 12:30:20 +03:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
validateHeaderName('');
|
|
|
|
|
} catch (err) {
|
2022-12-24 09:21:31 +09:00
|
|
|
|
console.error(err instanceof TypeError); // --> true
|
|
|
|
|
console.error(err.code); // --> 'ERR_INVALID_HTTP_TOKEN'
|
|
|
|
|
console.error(err.message); // --> 'Header name must be a valid HTTP token [""]'
|
2020-04-28 12:30:20 +03:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## `http.validateHeaderValue(name, value)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-04-28 12:30:20 +03:00
|
|
|
|
<!-- YAML
|
2020-05-17 18:45:37 -07:00
|
|
|
|
added: v14.3.0
|
2020-04-28 12:30:20 +03:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `name` {string}
|
|
|
|
|
* `value` {any}
|
|
|
|
|
|
|
|
|
|
Performs the low-level validations on the provided `value` that are done when
|
|
|
|
|
`res.setHeader(name, value)` is called.
|
|
|
|
|
|
|
|
|
|
Passing illegal value as `value` will result in a [`TypeError`][] being thrown.
|
2021-10-10 21:55:04 -07:00
|
|
|
|
|
2020-04-28 12:30:20 +03:00
|
|
|
|
* Undefined value error is identified by `code: 'ERR_HTTP_INVALID_HEADER_VALUE'`.
|
|
|
|
|
* Invalid value character error is identified by `code: 'ERR_INVALID_CHAR'`.
|
|
|
|
|
|
|
|
|
|
It is not necessary to use this method before passing headers to an HTTP request
|
|
|
|
|
or response. The HTTP module will automatically validate such headers.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
2023-04-28 19:26:55 +08:00
|
|
|
|
```mjs
|
|
|
|
|
import { validateHeaderValue } from 'node:http';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
validateHeaderValue('x-my-header', undefined);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err instanceof TypeError); // --> true
|
|
|
|
|
console.error(err.code === 'ERR_HTTP_INVALID_HEADER_VALUE'); // --> true
|
|
|
|
|
console.error(err.message); // --> 'Invalid value "undefined" for header "x-my-header"'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
validateHeaderValue('x-my-header', 'oʊmɪɡə');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err instanceof TypeError); // --> true
|
|
|
|
|
console.error(err.code === 'ERR_INVALID_CHAR'); // --> true
|
|
|
|
|
console.error(err.message); // --> 'Invalid character in header content ["x-my-header"]'
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
|
const { validateHeaderValue } = require('node:http');
|
2020-04-28 12:30:20 +03:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
validateHeaderValue('x-my-header', undefined);
|
|
|
|
|
} catch (err) {
|
2022-12-24 09:21:31 +09:00
|
|
|
|
console.error(err instanceof TypeError); // --> true
|
|
|
|
|
console.error(err.code === 'ERR_HTTP_INVALID_HEADER_VALUE'); // --> true
|
|
|
|
|
console.error(err.message); // --> 'Invalid value "undefined" for header "x-my-header"'
|
2020-04-28 12:30:20 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
validateHeaderValue('x-my-header', 'oʊmɪɡə');
|
|
|
|
|
} catch (err) {
|
2022-12-24 09:21:31 +09:00
|
|
|
|
console.error(err instanceof TypeError); // --> true
|
|
|
|
|
console.error(err.code === 'ERR_INVALID_CHAR'); // --> true
|
|
|
|
|
console.error(err.message); // --> 'Invalid character in header content ["x-my-header"]'
|
2020-04-28 12:30:20 +03:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-15 03:30:15 +05:30
|
|
|
|
## `http.setMaxIdleHTTPParsers(max)`
|
2022-07-29 17:00:38 +08:00
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-10-11 14:54:19 -05:00
|
|
|
|
added:
|
|
|
|
|
- v18.8.0
|
|
|
|
|
- v16.18.0
|
2022-07-29 17:00:38 +08:00
|
|
|
|
-->
|
|
|
|
|
|
2023-01-15 03:30:15 +05:30
|
|
|
|
* `max` {number} **Default:** `1000`.
|
2022-07-29 17:00:38 +08:00
|
|
|
|
|
2023-01-15 03:30:15 +05:30
|
|
|
|
Set the maximum number of idle HTTP parsers.
|
2022-07-29 17:00:38 +08:00
|
|
|
|
|
2024-07-08 17:55:43 +02:00
|
|
|
|
## `WebSocket`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
|
added:
|
2024-07-16 12:09:50 +02:00
|
|
|
|
- v22.5.0
|
2024-07-08 17:55:43 +02:00
|
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
A browser-compatible implementation of [`WebSocket`][].
|
|
|
|
|
|
2022-04-06 11:41:00 +02:00
|
|
|
|
[RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt
|
2022-08-24 22:19:29 +05:30
|
|
|
|
[`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`'checkContinue'`]: #event-checkcontinue
|
|
|
|
|
[`'finish'`]: #event-finish
|
|
|
|
|
[`'request'`]: #event-request
|
|
|
|
|
[`'response'`]: #event-response
|
|
|
|
|
[`'upgrade'`]: #event-upgrade
|
|
|
|
|
[`--insecure-http-parser`]: cli.md#--insecure-http-parser
|
|
|
|
|
[`--max-http-header-size`]: cli.md#--max-http-header-sizesize
|
|
|
|
|
[`Agent`]: #class-httpagent
|
|
|
|
|
[`Buffer.byteLength()`]: buffer.md#static-method-bufferbytelengthstring-encoding
|
|
|
|
|
[`Duplex`]: stream.md#class-streamduplex
|
|
|
|
|
[`HPE_HEADER_OVERFLOW`]: errors.md#hpe_header_overflow
|
2023-01-09 20:15:49 +01:00
|
|
|
|
[`Headers`]: globals.md#class-headers
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`TypeError`]: errors.md#class-typeerror
|
|
|
|
|
[`URL`]: url.md#the-whatwg-url-api
|
2025-02-05 11:01:18 +00:00
|
|
|
|
[`WebSocket`]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`agent.createConnection()`]: #agentcreateconnectionoptions-callback
|
|
|
|
|
[`agent.getName()`]: #agentgetnameoptions
|
|
|
|
|
[`destroy()`]: #agentdestroy
|
|
|
|
|
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
|
|
|
|
|
[`dns.lookup()` hints]: dns.md#supported-getaddrinfo-flags
|
|
|
|
|
[`getHeader(name)`]: #requestgetheadername
|
|
|
|
|
[`http.Agent`]: #class-httpagent
|
|
|
|
|
[`http.ClientRequest`]: #class-httpclientrequest
|
|
|
|
|
[`http.IncomingMessage`]: #class-httpincomingmessage
|
|
|
|
|
[`http.ServerResponse`]: #class-httpserverresponse
|
|
|
|
|
[`http.Server`]: #class-httpserver
|
2023-01-03 11:43:21 +01:00
|
|
|
|
[`http.createServer()`]: #httpcreateserveroptions-requestlistener
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`http.get()`]: #httpgetoptions-callback
|
|
|
|
|
[`http.globalAgent`]: #httpglobalagent
|
|
|
|
|
[`http.request()`]: #httprequestoptions-callback
|
|
|
|
|
[`message.headers`]: #messageheaders
|
|
|
|
|
[`message.socket`]: #messagesocket
|
2022-01-10 21:50:58 +01:00
|
|
|
|
[`message.trailers`]: #messagetrailers
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`net.Server.close()`]: net.md#serverclosecallback
|
|
|
|
|
[`net.Server`]: net.md#class-netserver
|
|
|
|
|
[`net.Socket`]: net.md#class-netsocket
|
|
|
|
|
[`net.createConnection()`]: net.md#netcreateconnectionoptions-connectlistener
|
|
|
|
|
[`new URL()`]: url.md#new-urlinput-base
|
2022-01-10 21:50:58 +01:00
|
|
|
|
[`outgoingMessage.setHeader(name, value)`]: #outgoingmessagesetheadername-value
|
2023-01-09 20:15:49 +01:00
|
|
|
|
[`outgoingMessage.setHeaders()`]: #outgoingmessagesetheadersheaders
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`outgoingMessage.socket`]: #outgoingmessagesocket
|
|
|
|
|
[`removeHeader(name)`]: #requestremoveheadername
|
|
|
|
|
[`request.destroy()`]: #requestdestroyerror
|
2021-01-19 07:55:57 +08:00
|
|
|
|
[`request.destroyed`]: #requestdestroyed
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`request.end()`]: #requestenddata-encoding-callback
|
|
|
|
|
[`request.flushHeaders()`]: #requestflushheaders
|
|
|
|
|
[`request.getHeader()`]: #requestgetheadername
|
|
|
|
|
[`request.setHeader()`]: #requestsetheadername-value
|
|
|
|
|
[`request.setTimeout()`]: #requestsettimeouttimeout-callback
|
|
|
|
|
[`request.socket.getPeerCertificate()`]: tls.md#tlssocketgetpeercertificatedetailed
|
|
|
|
|
[`request.socket`]: #requestsocket
|
|
|
|
|
[`request.writableEnded`]: #requestwritableended
|
|
|
|
|
[`request.writableFinished`]: #requestwritablefinished
|
|
|
|
|
[`request.write(data, encoding)`]: #requestwritechunk-encoding-callback
|
|
|
|
|
[`response.end()`]: #responseenddata-encoding-callback
|
|
|
|
|
[`response.getHeader()`]: #responsegetheadername
|
|
|
|
|
[`response.setHeader()`]: #responsesetheadername-value
|
|
|
|
|
[`response.socket`]: #responsesocket
|
2023-02-14 20:55:52 +01:00
|
|
|
|
[`response.strictContentLength`]: #responsestrictcontentlength
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`response.writableEnded`]: #responsewritableended
|
|
|
|
|
[`response.writableFinished`]: #responsewritablefinished
|
|
|
|
|
[`response.write()`]: #responsewritechunk-encoding-callback
|
|
|
|
|
[`response.write(data, encoding)`]: #responsewritechunk-encoding-callback
|
|
|
|
|
[`response.writeContinue()`]: #responsewritecontinue
|
|
|
|
|
[`response.writeHead()`]: #responsewriteheadstatuscode-statusmessage-headers
|
2023-06-25 20:19:40 +03:00
|
|
|
|
[`server.close()`]: #serverclosecallback
|
2022-04-13 16:47:59 +02:00
|
|
|
|
[`server.headersTimeout`]: #serverheaderstimeout
|
|
|
|
|
[`server.keepAliveTimeout`]: #serverkeepalivetimeout
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`server.listen()`]: net.md#serverlisten
|
2022-04-13 16:47:59 +02:00
|
|
|
|
[`server.requestTimeout`]: #serverrequesttimeout
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`server.timeout`]: #servertimeout
|
|
|
|
|
[`setHeader(name, value)`]: #requestsetheadername-value
|
|
|
|
|
[`socket.connect()`]: net.md#socketconnectoptions-connectlistener
|
|
|
|
|
[`socket.setKeepAlive()`]: net.md#socketsetkeepaliveenable-initialdelay
|
|
|
|
|
[`socket.setNoDelay()`]: net.md#socketsetnodelaynodelay
|
|
|
|
|
[`socket.setTimeout()`]: net.md#socketsettimeouttimeout-callback
|
|
|
|
|
[`socket.unref()`]: net.md#socketunref
|
2023-04-24 13:35:38 +08:00
|
|
|
|
[`stream.getDefaultHighWaterMark()`]: stream.md#streamgetdefaulthighwatermarkobjectmode
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[`url.parse()`]: url.md#urlparseurlstring-parsequerystring-slashesdenotehost
|
|
|
|
|
[`writable.cork()`]: stream.md#writablecork
|
|
|
|
|
[`writable.destroy()`]: stream.md#writabledestroyerror
|
|
|
|
|
[`writable.destroyed`]: stream.md#writabledestroyed
|
|
|
|
|
[`writable.uncork()`]: stream.md#writableuncork
|
2022-04-17 21:23:55 +02:00
|
|
|
|
[`writable.write()`]: stream.md#writablewritechunk-encoding-callback
|
2021-07-04 20:39:17 -07:00
|
|
|
|
[initial delay]: net.md#socketsetkeepaliveenable-initialdelay
|