2012-02-27 20:09:33 +01:00
|
|
|
# HTTP
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
To use the HTTP server and client one must `require('http')`.
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
The HTTP interfaces in Node.js are designed to support many features
|
2010-10-28 14:18:16 +02:00
|
|
|
of the protocol which have been traditionally difficult to use.
|
|
|
|
In particular, large, possibly chunk-encoded, messages. The interface is
|
2020-03-04 06:23:59 +01:00
|
|
|
careful to never buffer entire requests or responses, so the
|
2010-10-28 14:18:16 +02:00
|
|
|
user is able to stream data.
|
|
|
|
|
|
|
|
HTTP message headers are represented by an object like this:
|
|
|
|
|
2017-07-03 02:05:59 +02:00
|
|
|
<!-- eslint-skip -->
|
2016-07-09 07:13:09 +02:00
|
|
|
```js
|
2016-01-17 18:39:07 +01:00
|
|
|
{ 'content-length': '123',
|
|
|
|
'content-type': 'text/plain',
|
|
|
|
'connection': 'keep-alive',
|
|
|
|
'host': 'mysite.com',
|
|
|
|
'accept': '*/*' }
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Keys are lowercased. Values are not modified.
|
|
|
|
|
2020-02-12 02:03:31 +01:00
|
|
|
In order to support the full spectrum of possible HTTP applications, the Node.js
|
2010-10-28 14:18:16 +02: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-28 00:30:32 +01:00
|
|
|
See [`message.headers`][] for details on how duplicate headers are handled.
|
2013-08-15 23:12:12 +02:00
|
|
|
|
|
|
|
The raw headers as they were received are retained in the `rawHeaders`
|
2018-04-02 07:38:48 +02:00
|
|
|
property, which is an array of `[key, value, key2, value2, ...]`. For
|
2013-08-15 23:12:12 +02:00
|
|
|
example, the previous message header object might have a `rawHeaders`
|
|
|
|
list like the following:
|
|
|
|
|
2017-04-21 21:55:51 +02:00
|
|
|
<!-- eslint-disable semi -->
|
2016-07-09 07:13:09 +02:00
|
|
|
```js
|
2016-01-17 18:39:07 +01:00
|
|
|
[ 'ConTent-Length', '123456',
|
|
|
|
'content-LENGTH', '123',
|
|
|
|
'content-type', 'text/plain',
|
|
|
|
'CONNECTION', 'keep-alive',
|
|
|
|
'Host', 'mysite.com',
|
|
|
|
'accepT', '*/*' ]
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## Class: `http.Agent`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.4
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-09 18:45:46 +01: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
|
|
|
|
`keepAlive` [option](#http_new_agent_options).
|
|
|
|
|
|
|
|
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 06:31:57 +02:00
|
|
|
(see [`socket.unref()`][]).
|
2017-01-09 18:45:46 +01: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 11:56:03 +02:00
|
|
|
Sockets are removed from an agent when the socket emits either
|
2017-03-04 00:45:20 +01:00
|
|
|
a `'close'` event or an `'agentRemove'` event. When intending to keep one
|
2017-08-04 11:56:03 +02:00
|
|
|
HTTP request open for a long time without keeping it in the agent, something
|
2017-03-04 00:45:20 +01:00
|
|
|
like the following may be done:
|
2010-10-28 14:18:16 +02: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 14:18:16 +02:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
An agent may also be used for an individual request. By providing
|
2017-01-09 18:45:46 +01: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 23:56:03 +01:00
|
|
|
`agent:false`:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
http.get({
|
|
|
|
hostname: 'localhost',
|
|
|
|
port: 80,
|
|
|
|
path: '/',
|
2019-01-21 01:22:27 +01: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-15 07:41:29 +02:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `new Agent([options])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.4
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 18:45:46 +01: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-04 00:45:20 +01:00
|
|
|
* `keepAliveMsecs` {number} When using the `keepAlive` option, specifies
|
2017-02-01 20:21:46 +01:00
|
|
|
the [initial delay](net.html#net_socket_setkeepalive_enable_initialdelay)
|
2017-01-09 18:45:46 +01:00
|
|
|
for TCP Keep-Alive packets. Ignored when the
|
2018-04-02 03:44:32 +02:00
|
|
|
`keepAlive` option is `false` or `undefined`. **Default:** `1000`.
|
2017-02-04 16:15:33 +01:00
|
|
|
* `maxSockets` {number} Maximum number of sockets to allow per
|
2019-03-03 12:28:17 +01:00
|
|
|
host. Each request will use a new socket until the maximum is reached.
|
|
|
|
**Default:** `Infinity`.
|
2017-02-04 16:15:33 +01:00
|
|
|
* `maxFreeSockets` {number} Maximum number of sockets to leave open
|
2018-04-02 07:38:48 +02:00
|
|
|
in a free state. Only relevant if `keepAlive` is set to `true`.
|
2018-04-02 03:44:32 +02:00
|
|
|
**Default:** `256`.
|
2018-06-08 06:42:27 +02: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 14:18:16 +02:00
|
|
|
|
2018-12-05 07:41:19 +01:00
|
|
|
`options` in [`socket.connect()`][] are also supported.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The default [`http.globalAgent`][] that is used by [`http.request()`][] has all
|
2015-11-04 23:56:03 +01:00
|
|
|
of these values set to their respective defaults.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
To configure any of them, a custom [`http.Agent`][] instance must be created.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2015-12-15 00:20:25 +01:00
|
|
|
const http = require('http');
|
2017-04-02 20:39:42 +02:00
|
|
|
const keepAliveAgent = new http.Agent({ keepAlive: true });
|
2015-11-04 23:56:03 +01:00
|
|
|
options.agent = keepAliveAgent;
|
|
|
|
http.request(options, onResponseCallback);
|
|
|
|
```
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.createConnection(options[, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.4
|
|
|
|
-->
|
2016-01-12 05:41:01 +01:00
|
|
|
|
2016-09-11 01:03:30 +02: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-11 01:03:30 +02:00
|
|
|
|
2016-01-12 05:41:01 +01: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 18:45:46 +01:00
|
|
|
custom agents may override this method in case greater flexibility is desired.
|
2016-01-12 05:41:01 +01: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-12 05:41:01 +01:00
|
|
|
`callback` has a signature of `(err, stream)`.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.keepSocketAlive(socket)`
|
2017-05-12 22:53:15 +02:00
|
|
|
<!-- YAML
|
2017-11-16 15:32:56 +01:00
|
|
|
added: v8.1.0
|
2017-05-12 22:53:15 +02:00
|
|
|
-->
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* `socket` {stream.Duplex}
|
2017-05-12 22:53:15 +02:00
|
|
|
|
|
|
|
Called when `socket` is detached from a request and could be persisted by the
|
2018-04-29 19:46:41 +02:00
|
|
|
`Agent`. Default behavior is to:
|
2017-05-12 22:53:15 +02:00
|
|
|
|
|
|
|
```js
|
2017-08-04 11:56:03 +02:00
|
|
|
socket.setKeepAlive(true, this.keepAliveMsecs);
|
2017-05-12 22:53:15 +02:00
|
|
|
socket.unref();
|
2017-08-04 11:56:03 +02:00
|
|
|
return true;
|
2017-05-12 22:53:15 +02: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 12:53:13 +01:00
|
|
|
### `agent.reuseSocket(socket, request)`
|
2017-05-12 22:53:15 +02:00
|
|
|
<!-- YAML
|
2017-11-16 15:32:56 +01:00
|
|
|
added: v8.1.0
|
2017-05-12 22:53:15 +02:00
|
|
|
-->
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* `socket` {stream.Duplex}
|
2017-05-12 22:53:15 +02: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 12:53:13 +01:00
|
|
|
### `agent.destroy()`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.4
|
|
|
|
-->
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Destroy any sockets that are currently in use by the agent.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
It is usually not necessary to do this. However, if using an
|
2017-01-09 18:45:46 +01:00
|
|
|
agent with `keepAlive` enabled, then it is best to explicitly shut down
|
2018-04-02 07:38:48 +02:00
|
|
|
the agent when it will no longer be used. Otherwise,
|
2015-11-04 23:56:03 +01:00
|
|
|
sockets may hang open for quite a long time before the server
|
|
|
|
terminates them.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.freeSockets`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.4
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* {Object}
|
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
An object which contains arrays of sockets currently awaiting use by
|
2018-04-02 07:38:48 +02:00
|
|
|
the agent when `keepAlive` is enabled. Do not modify.
|
2010-10-28 14:18:16 +02: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'`.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.getName(options)`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.4
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `options` {Object} A set of options providing information for name generation
|
2018-02-12 08:31:55 +01: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-11 01:03:30 +02:00
|
|
|
when issuing the request
|
2017-08-04 11:56:03 +02:00
|
|
|
* `family` {integer} Must be 4 or 6 if this doesn't equal `undefined`.
|
2017-03-04 00:45:20 +01:00
|
|
|
* Returns: {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Get a unique name for a set of request options, to determine whether a
|
2017-08-04 11:56:03 +02: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-15 20:45:31 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.maxFreeSockets`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.7
|
|
|
|
-->
|
2012-01-15 20:45:31 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {number}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
By default set to 256. For agents with `keepAlive` enabled, this
|
2015-11-04 23:56:03 +01:00
|
|
|
sets the maximum number of sockets that will be left open in the free
|
|
|
|
state.
|
2012-01-15 20:45:31 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.maxSockets`
|
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-04 08:29:22 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {number}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
By default set to `Infinity`. Determines how many concurrent sockets the agent
|
2017-08-04 11:56:03 +02: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-04 08:29:22 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.requests`
|
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-04 08:29:22 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* {Object}
|
|
|
|
|
2015-11-04 23:56:03 +01: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-04 08:29:22 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `agent.sockets`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.6
|
|
|
|
-->
|
2015-05-14 04:25:57 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* {Object}
|
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
An object which contains arrays of sockets currently in use by the
|
2018-04-02 07:38:48 +02: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-04 08:29:22 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## Class: `http.ClientRequest`
|
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-04 08:29:22 +01:00
|
|
|
|
2019-08-21 22:11:16 +02:00
|
|
|
* Extends: {Stream}
|
|
|
|
|
2018-04-02 07:38:48 +02: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)`][],
|
2018-04-02 07:38:48 +02: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-04 08:29:22 +01:00
|
|
|
|
2016-07-09 07:13:09 +02: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 07:38:48 +02:00
|
|
|
headers have been received. The [`'response'`][] event is executed with one
|
2015-11-28 00:30:32 +01: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-04 08:29:22 +01:00
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
During the [`'response'`][] event, one can add listeners to the
|
2015-11-04 23:56:03 +01:00
|
|
|
response object; particularly to listen for the `'data'` event.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
If no [`'response'`][] handler is added, then the response will be
|
2018-04-02 07:38:48 +02:00
|
|
|
entirely discarded. However, if a [`'response'`][] event handler is added,
|
2017-03-04 00:45:20 +01:00
|
|
|
then the data from the response object **must** be consumed, either by
|
2015-11-04 23:56:03 +01: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 07:38:48 +02:00
|
|
|
Until the data is consumed, the `'end'` event will not fire. Also, until
|
2015-11-04 23:56:03 +01:00
|
|
|
the data is read it will consume memory that can eventually lead to a
|
|
|
|
'process out of memory' error.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-06-17 10:26:52 +02:00
|
|
|
Unlike the `request` object, if the response closes prematurely, the
|
|
|
|
`response` object does not emit an `'error'` event but instead emits the
|
|
|
|
`'aborted'` event.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
Node.js does not check whether Content-Length and the length of the
|
2017-05-20 22:15:58 +02:00
|
|
|
body which has been transmitted are equal or not.
|
2012-02-05 11:11:54 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'abort'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.4.1
|
|
|
|
-->
|
2013-11-26 21:49:28 +01:00
|
|
|
|
2015-11-04 23:56:03 +01: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 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'connect'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `response` {http.IncomingMessage}
|
2019-10-28 16:47:07 +01:00
|
|
|
* `socket` {stream.Duplex}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `head` {Buffer}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-02-12 08:31:55 +01: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 14:18:16 +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}.
|
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
A client and server pair demonstrating how to listen for the `'connect'` event:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const http = require('http');
|
|
|
|
const net = require('net');
|
2019-10-10 11:05:48 +02:00
|
|
|
const { URL } = require('url');
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
// Create an HTTP tunneling proxy
|
2017-04-21 16:38:31 +02:00
|
|
|
const proxy = http.createServer((req, res) => {
|
2017-06-01 01:07:25 +02:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-01-17 18:39:07 +01:00
|
|
|
res.end('okay');
|
|
|
|
});
|
2020-01-07 11:11:11 +01: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 11:11:11 +01: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 11:11:11 +01: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 20:39:42 +02:00
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
port: 1337,
|
2018-11-06 18:58:42 +01:00
|
|
|
host: '127.0.0.1',
|
2016-01-17 18:39:07 +01:00
|
|
|
method: 'CONNECT',
|
|
|
|
path: 'www.google.com:80'
|
|
|
|
};
|
|
|
|
|
2017-04-02 20:39:42 +02: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 23:56:03 +01:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
socket.on('end', () => {
|
|
|
|
proxy.close();
|
2015-11-04 23:56:03 +01: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-04 08:29:22 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'continue'`
|
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-04 08:29:22 +01:00
|
|
|
|
2015-11-04 23:56:03 +01: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 11:18:13 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'information'`
|
2018-01-08 09:11:05 +01:00
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
added: v10.0.0
|
2018-01-08 09:11:05 +01:00
|
|
|
-->
|
|
|
|
|
2019-03-30 09:55:28 +01:00
|
|
|
* `info` {Object}
|
2019-06-28 01:10:55 +02:00
|
|
|
* `httpVersion` {string}
|
|
|
|
* `httpVersionMajor` {integer}
|
|
|
|
* `httpVersionMinor` {integer}
|
2019-03-30 09:55:28 +01:00
|
|
|
* `statusCode` {integer}
|
2019-06-28 01:10:55 +02:00
|
|
|
* `statusMessage` {string}
|
|
|
|
* `headers` {Object}
|
|
|
|
* `rawHeaders` {string[]}
|
|
|
|
|
|
|
|
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 09:11:05 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
const http = require('http');
|
|
|
|
|
|
|
|
const options = {
|
2018-11-06 18:58:42 +01:00
|
|
|
host: '127.0.0.1',
|
2018-01-08 09:11:05 +01:00
|
|
|
port: 8080,
|
|
|
|
path: '/length_request'
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 09:11:05 +01: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 12:53:13 +01:00
|
|
|
### Event: `'response'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.0
|
|
|
|
-->
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `response` {http.IncomingMessage}
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Emitted when a response is received to this request. This event is emitted only
|
2016-09-11 01:03:30 +02:00
|
|
|
once.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'socket'`
|
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 12:53:13 +01:00
|
|
|
### Event: `'timeout'`
|
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
|
|
|
|
that the socket has been idle. The request must be aborted manually.
|
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
See also: [`request.setTimeout()`][].
|
2017-09-17 09:30:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'upgrade'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
|
|
|
-->
|
2013-10-17 02:11:19 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `response` {http.IncomingMessage}
|
2019-10-28 16:47:07 +01:00
|
|
|
* `socket` {stream.Duplex}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `head` {Buffer}
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2015-11-04 23:56:03 +01: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 11:18:13 +01: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-04 00:45:20 +01:00
|
|
|
A client server pair demonstrating how to listen for the `'upgrade'` event.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const http = require('http');
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// Create an HTTP server
|
2020-01-07 11:11:11 +01:00
|
|
|
const server = http.createServer((req, res) => {
|
2017-06-01 01:07:25 +02:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-01-17 18:39:07 +01:00
|
|
|
res.end('okay');
|
|
|
|
});
|
2020-01-07 11:11:11 +01: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 11:11:11 +01:00
|
|
|
server.listen(1337, '127.0.0.1', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
// make a request
|
2017-04-02 20:39:42 +02:00
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
port: 1337,
|
2018-11-06 18:58:42 +01:00
|
|
|
host: '127.0.0.1',
|
2016-01-17 18:39:07 +01:00
|
|
|
headers: {
|
|
|
|
'Connection': 'Upgrade',
|
|
|
|
'Upgrade': 'websocket'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-02 20:39:42 +02: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-14 21:38:24 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.abort()`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.8
|
|
|
|
-->
|
2012-02-14 21:38:24 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Marks the request as aborting. Calling this will cause remaining data
|
2019-07-15 16:57:40 +02:00
|
|
|
in the response to be dropped and the socket to be destroyed.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.aborted`
|
2017-02-24 21:47:18 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.14
|
2018-04-26 09:53:54 +02:00
|
|
|
changes:
|
2018-10-03 01:01:19 +02: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 21:47:18 +01:00
|
|
|
-->
|
|
|
|
|
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 21:47:18 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.connection`
|
2017-06-11 20:18:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
2019-09-09 14:32:18 +02:00
|
|
|
deprecated: v13.0.0
|
2017-06-11 20:18:03 +02: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 20:18:03 +02:00
|
|
|
|
2018-04-29 13:16:44 +02:00
|
|
|
See [`request.socket`][].
|
2017-06-11 20:18:03 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.end([data[, encoding]][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
2018-02-14 13:32:01 +01:00
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
- version: v10.0.0
|
2018-02-14 13:32:01 +01: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 11:18:13 +01:00
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
* `data` {string|Buffer}
|
2017-02-04 16:15:33 +01:00
|
|
|
* `encoding` {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
2018-02-14 13:32:01 +01:00
|
|
|
* Returns: {this}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 11:18:13 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
If `data` is specified, it is equivalent to calling
|
2017-07-07 18:05:08 +02:00
|
|
|
[`request.write(data, encoding)`][] followed by `request.end(callback)`.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
If `callback` is specified, it will be called when the request stream
|
|
|
|
is finished.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.finished`
|
2018-11-12 12:36:32 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
2019-12-13 17:02:36 +01:00
|
|
|
deprecated: v13.4.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 12:53:13 +01:00
|
|
|
### `request.flushHeaders()`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.6.0
|
|
|
|
-->
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2019-07-22 17:30:20 +02:00
|
|
|
Flushes the request headers.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2017-03-04 00:45:20 +01: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 11:18:13 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
That's usually desired (it saves a TCP round-trip), but not when the first
|
2018-04-02 07:38:48 +02:00
|
|
|
data is not sent until possibly much later. `request.flushHeaders()` bypasses
|
2017-03-04 00:45:20 +01:00
|
|
|
the optimization and kickstarts the request.
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.getHeader(name)`
|
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 21:52:54 +02: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
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.maxHeadersCount`
|
2018-04-23 02:07:00 +02:00
|
|
|
|
|
|
|
* {number} **Default:** `2000`
|
|
|
|
|
|
|
|
Limits maximum response headers count. If set to 0, no limit will be applied.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.path`
|
2019-01-29 11:40:45 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.4.0
|
|
|
|
-->
|
|
|
|
|
2019-02-22 11:27:41 +01:00
|
|
|
* {string} The request path.
|
2019-01-29 11:40:45 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.removeHeader(name)`
|
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 12:53:13 +01:00
|
|
|
### `request.reusedSocket`
|
2019-09-26 05:56:59 +02:00
|
|
|
|
|
|
|
<!-- YAML
|
2019-09-09 14:32:18 +02:00
|
|
|
added: v13.0.0
|
2019-09-26 05:56:59 +02: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.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const http = require('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
|
|
|
|
```
|
|
|
|
|
|
|
|
By marking a request whether it reused socket or not, we can do
|
|
|
|
automatic error retry base on it.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const http = require('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();
|
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.setHeader(name, value)`
|
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
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.setNoDelay([noDelay])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.9
|
|
|
|
-->
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `noDelay` {boolean}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Once a socket is assigned to this request and is connected
|
2015-11-28 00:30:32 +01:00
|
|
|
[`socket.setNoDelay()`][] will be called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.setSocketKeepAlive([enable][, initialDelay])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.9
|
|
|
|
-->
|
2011-02-10 11:18:13 +01:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `enable` {boolean}
|
|
|
|
* `initialDelay` {number}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Once a socket is assigned to this request and is connected
|
2015-11-28 00:30:32 +01:00
|
|
|
[`socket.setKeepAlive()`][] will be called.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.setTimeout(timeout[, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.9
|
2018-12-19 04:59:02 +01: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 14:18:16 +02:00
|
|
|
|
2018-01-11 05:07:01 +01:00
|
|
|
* `timeout` {number} Milliseconds before a request times out.
|
2018-02-12 08:31:55 +01:00
|
|
|
* `callback` {Function} Optional function to be called when a timeout occurs.
|
2018-04-09 18:30:22 +02:00
|
|
|
Same as binding to the `'timeout'` event.
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {http.ClientRequest}
|
2015-12-29 00:10:21 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
Once a socket is assigned to this request and is connected
|
|
|
|
[`socket.setTimeout()`][] will be called.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.socket`
|
2017-06-11 20:18:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* {stream.Duplex}
|
2017-06-11 20:18:03 +02:00
|
|
|
|
|
|
|
Reference to the underlying socket. Usually users will not want to access
|
|
|
|
this property. In particular, the socket will not emit `'readable'` events
|
2018-04-12 18:31:03 +02:00
|
|
|
because of how the protocol parser attaches to the socket. The `socket`
|
|
|
|
may also be accessed via `request.connection`.
|
2017-06-11 20:18:03 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
const http = require('http');
|
2017-08-04 11:56:03 +02: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 11:56:03 +02:00
|
|
|
});
|
2017-06-11 20:18:03 +02: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}.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `request.writableEnded`
|
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 12:53:13 +01:00
|
|
|
### `request.writableFinished`
|
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 12:53:13 +01:00
|
|
|
### `request.write(chunk[, encoding][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.29
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
* `chunk` {string|Buffer}
|
2017-02-04 16:15:33 +01:00
|
|
|
* `encoding` {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {boolean}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
Sends a chunk of the body. By calling this method
|
2017-03-04 00:45:20 +01:00
|
|
|
many times, a request body can be sent to a
|
2020-03-04 06:23:59 +01:00
|
|
|
server. In that case, it is suggested to use the
|
2015-11-04 23:56:03 +01:00
|
|
|
`['Transfer-Encoding', 'chunked']` header line when
|
|
|
|
creating the request.
|
2012-07-31 06:15:46 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
The `encoding` argument is optional and only applies when `chunk` is a string.
|
|
|
|
Defaults to `'utf8'`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
The `callback` argument is optional and will be called when this chunk of data
|
2018-08-22 20:01:23 +02:00
|
|
|
is flushed, but only if the chunk is non-empty.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-02-02 11:40:01 +01: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 14:18:16 +02:00
|
|
|
|
2018-08-21 16:20:04 +02:00
|
|
|
When `write` function is called with empty string or buffer, it does
|
|
|
|
nothing and waits for more input.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## Class: `http.Server`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.17
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-08-21 22:11:16 +02:00
|
|
|
* Extends: {net.Server}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'checkContinue'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `request` {http.IncomingMessage}
|
|
|
|
* `response` {http.ServerResponse}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
Emitted each time a request with an HTTP `Expect: 100-continue` is received.
|
2017-01-24 20:37:46 +01:00
|
|
|
If this event is not listened for, the server will automatically respond
|
2016-09-11 01:03:30 +02:00
|
|
|
with a `100 Continue` as appropriate.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-02-12 08:31:55 +01: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 14:18:16 +02:00
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
When this event is emitted and handled, the [`'request'`][] event will
|
2016-09-11 01:03:30 +02:00
|
|
|
not be emitted.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'checkExpectation'`
|
2016-09-11 01:03:30 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v5.5.0
|
|
|
|
-->
|
|
|
|
|
2017-08-04 11:56:03 +02:00
|
|
|
* `request` {http.IncomingMessage}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `response` {http.ServerResponse}
|
|
|
|
|
|
|
|
Emitted each time a request with an HTTP `Expect` header is received, where the
|
2017-01-24 20:37:46 +01:00
|
|
|
value is not `100-continue`. If this event is not listened for, the server will
|
2016-09-11 01:03:30 +02:00
|
|
|
automatically respond with a `417 Expectation Failed` as appropriate.
|
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
When this event is emitted and handled, the [`'request'`][] event will
|
2015-11-04 23:56:03 +01:00
|
|
|
not be emitted.
|
2013-12-10 11:56:04 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'clientError'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.94
|
2017-02-21 23:38:45 +01:00
|
|
|
changes:
|
|
|
|
- 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
|
2018-04-09 18:30:22 +02:00
|
|
|
for `'clientError'`.
|
2018-01-10 01:23:55 +01:00
|
|
|
- version: v9.4.0
|
2017-12-14 05:40:32 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/17672
|
2018-04-29 19:46:41 +02:00
|
|
|
description: The `rawPacket` is the current buffer that just parsed. Adding
|
2018-04-09 18:30:22 +02:00
|
|
|
this buffer to the error object of `'clientError'` event is to
|
|
|
|
make it possible that developers can log the broken packet.
|
2019-03-22 14:19:46 +01:00
|
|
|
- version: v12.0.0
|
2019-01-21 07:47:32 +01:00
|
|
|
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.
|
2016-06-23 21:50:36 +02:00
|
|
|
-->
|
2013-12-10 11:56:04 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `exception` {Error}
|
2019-10-28 16:47:07 +01:00
|
|
|
* `socket` {stream.Duplex}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
If a client connection emits an `'error'` event, it will be forwarded here.
|
2016-01-06 23:00:27 +01: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
|
|
|
|
custom HTTP response instead of abruptly severing the connection.
|
2016-01-06 23:00:27 +01: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 07:47:32 +01: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
|
|
|
|
[`HPE_HEADER_OVERFLOW`][] error. If the socket is not writable it is
|
|
|
|
immediately destroyed.
|
2012-05-16 16:27:34 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
`socket` is the [`net.Socket`][] object that the error originated from.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-02-15 23:48:00 +01:00
|
|
|
```js
|
|
|
|
const http = require('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);
|
|
|
|
```
|
|
|
|
|
|
|
|
When the `'clientError'` event occurs, there is no `request` or `response`
|
|
|
|
object, so any HTTP response sent, including response headers and payload,
|
|
|
|
*must* be written directly to the `socket` object. Care must be taken to
|
|
|
|
ensure the response is a properly formatted HTTP response message.
|
|
|
|
|
2017-12-14 05:40:32 +01:00
|
|
|
`err` is an instance of `Error` with two extra columns:
|
|
|
|
|
2019-09-13 06:22:29 +02:00
|
|
|
* `bytesParsed`: the bytes count of request packet that Node.js may have parsed
|
2017-12-14 05:40:32 +01:00
|
|
|
correctly;
|
2019-09-13 06:22:29 +02:00
|
|
|
* `rawPacket`: the raw packet of current request.
|
2017-12-14 05:40:32 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'close'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.4
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Emitted when the server closes.
|
2013-11-28 21:25:30 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'connect'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2016-09-11 01:03:30 +02: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-11 01:03:30 +02:00
|
|
|
* `head` {Buffer} The first packet of the tunneling stream (may be empty)
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2017-01-24 20:37:46 +01: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 23:56:03 +01:00
|
|
|
connections closed.
|
2014-09-12 11:25:15 +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}.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
After this event is emitted, the request's socket will not have a `'data'`
|
2017-03-04 00:45:20 +01:00
|
|
|
event listener, meaning it will need to be bound in order to handle data
|
2015-11-04 23:56:03 +01:00
|
|
|
sent to the server on that socket.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'connection'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.0
|
|
|
|
-->
|
2011-05-06 00:40:45 +02:00
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* `socket` {stream.Duplex}
|
2011-01-21 02:54:59 +01: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
|
|
|
|
also be accessed at `request.connection`.
|
|
|
|
|
2018-02-06 06:55:16 +01: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 14:18:16 +02:00
|
|
|
|
2019-01-28 00:09:17 +01: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}.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'request'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.0
|
|
|
|
-->
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `request` {http.IncomingMessage}
|
|
|
|
* `response` {http.ServerResponse}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
Emitted each time there is a request. There may be multiple requests
|
2017-01-09 18:45:46 +01:00
|
|
|
per connection (in the case of HTTP Keep-Alive connections).
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'upgrade'`
|
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 18:53:46 +01:00
|
|
|
- version: v10.0.0
|
|
|
|
pr-url: v10.0.0
|
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 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02: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-11 01:03:30 +02:00
|
|
|
* `head` {Buffer} The first packet of the upgraded stream (may be empty)
|
2010-10-28 14:18:16 +02: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-20 00:06:49 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
After this event is emitted, the request's socket will not have a `'data'`
|
2017-03-04 00:45:20 +01:00
|
|
|
event listener, meaning it will need to be bound in order to handle data
|
2015-11-04 23:56:03 +01:00
|
|
|
sent to the server on that socket.
|
2011-01-21 02:54:59 +01: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 12:53:13 +01:00
|
|
|
### `server.close([callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
Stops the server from accepting new connections. See [`net.Server.close()`][].
|
2011-01-21 02:54:59 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.headersTimeout`
|
2018-11-28 15:48:07 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v11.3.0
|
|
|
|
-->
|
|
|
|
|
2019-10-23 00:17:03 +02:00
|
|
|
* {number} **Default:** `60000`
|
2018-11-28 15:48:07 +01:00
|
|
|
|
|
|
|
Limit the amount of time the parser will wait to receive the complete HTTP
|
|
|
|
headers.
|
|
|
|
|
|
|
|
In case of inactivity, the rules defined in [`server.timeout`][] apply. However,
|
|
|
|
that inactivity based timeout would still allow the connection to be kept open
|
|
|
|
if the headers are being sent very slowly (by default, up to a byte per 2
|
|
|
|
minutes). In order to prevent this, whenever header data arrives an additional
|
|
|
|
check is made that more than `server.headersTimeout` milliseconds has not
|
|
|
|
passed since the connection was established. If the check fails, a `'timeout'`
|
|
|
|
event is emitted on the server object, and (by default) the socket is destroyed.
|
|
|
|
See [`server.timeout`][] for more information on how timeout behavior can be
|
|
|
|
customized.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.listen()`
|
2016-08-27 06:26:28 +02:00
|
|
|
|
2017-10-06 20:50:47 +02:00
|
|
|
Starts the HTTP server listening for connections.
|
|
|
|
This method is identical to [`server.listen()`][] from [`net.Server`][].
|
2013-08-05 06:30:23 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.listening`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v5.7.0
|
|
|
|
-->
|
2016-01-18 14:36:48 +01:00
|
|
|
|
2018-04-29 19:46:41 +02:00
|
|
|
* {boolean} Indicates whether or not the server is listening for connections.
|
2016-01-18 14:36:48 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.maxHeadersCount`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
* {number} **Default:** `2000`
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2018-04-23 02:07:00 +02:00
|
|
|
Limits maximum incoming headers count. If set to 0, no limit will be applied.
|
2013-08-05 06:30:23 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.setTimeout([msecs][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.12
|
2019-05-04 01:45:57 +02:00
|
|
|
changes:
|
2019-09-09 14:32:18 +02:00
|
|
|
- version: v13.0.0
|
2019-05-04 01:45:57 +02: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-05 06:30:23 +02:00
|
|
|
|
2019-05-04 01:45:57 +02:00
|
|
|
* `msecs` {number} **Default:** 0 (no timeout)
|
2015-11-04 23:56:03 +01:00
|
|
|
* `callback` {Function}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {http.Server}
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 23:56:03 +01: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-05 06:30:23 +02:00
|
|
|
|
2019-05-04 01:45:57 +02: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-05 06:30:23 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.timeout`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.12
|
2019-05-04 01:45:57 +02:00
|
|
|
changes:
|
2019-09-09 14:32:18 +02:00
|
|
|
- version: v13.0.0
|
2019-05-04 01:45:57 +02: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-04 01:45:57 +02:00
|
|
|
* {number} Timeout in milliseconds. **Default:** 0 (no timeout)
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
The number of milliseconds of inactivity before a socket is presumed
|
|
|
|
to have timed out.
|
2013-08-05 06:30:23 +02:00
|
|
|
|
2017-12-13 19:47:30 +01:00
|
|
|
A value of `0` will disable the timeout behavior on incoming connections.
|
2013-08-05 06:30:23 +02:00
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
The socket timeout logic is set up on connection, so changing this
|
2015-10-29 20:53:43 +01:00
|
|
|
value only affects new connections to the server, not any existing connections.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `server.keepAliveTimeout`
|
2015-10-29 20:53:43 +01:00
|
|
|
<!-- YAML
|
2017-03-16 04:26:14 +01:00
|
|
|
added: v8.0.0
|
2015-10-29 20:53:43 +01:00
|
|
|
-->
|
|
|
|
|
2018-04-02 03:44:32 +02:00
|
|
|
* {number} Timeout in milliseconds. **Default:** `5000` (5 seconds).
|
2015-10-29 20:53:43 +01: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 08:31:55 +01: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 20:53:43 +01:00
|
|
|
|
2018-02-06 06:55:16 +01: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-05 06:30:23 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## Class: `http.ServerResponse`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.17
|
|
|
|
-->
|
2013-08-05 06:30:23 +02:00
|
|
|
|
2019-08-21 22:11:16 +02:00
|
|
|
* Extends: {Stream}
|
|
|
|
|
2020-03-04 06:23:59 +01:00
|
|
|
This object is created internally by an HTTP server, not by the user. It is
|
2016-09-11 01:03:30 +02:00
|
|
|
passed as the second parameter to the [`'request'`][] event.
|
2013-08-05 06:30:23 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'close'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.6.7
|
|
|
|
-->
|
2012-02-27 20:09:33 +01:00
|
|
|
|
2018-05-30 22:55:45 +02:00
|
|
|
Indicates that the underlying connection was terminated.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'finish'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.6
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 12:53:13 +01:00
|
|
|
### `response.addTrailers(headers)`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* `headers` {Object}
|
|
|
|
|
2015-11-04 23:56:03 +01: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 23:56:03 +01:00
|
|
|
Trailers will **only** be emitted if chunked encoding is used for the
|
2016-11-12 08:19:47 +01:00
|
|
|
response; if it is not (e.g. if the request was HTTP/1.0), they will
|
2015-11-04 23:56:03 +01:00
|
|
|
be silently discarded.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
HTTP requires the `Trailer` header to be sent in order to
|
2015-11-04 23:56:03 +01: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 01:07:25 +02: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-04 02:32:23 +01:00
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
|
|
|
will result in a [`TypeError`][] being thrown.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.connection`
|
2017-06-11 20:18:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
2019-09-09 14:32:18 +02:00
|
|
|
deprecated: v13.0.0
|
2017-06-11 20:18:03 +02:00
|
|
|
-->
|
|
|
|
|
2019-08-06 13:56:52 +02:00
|
|
|
> Stability: 0 - Deprecated. Use [`response.socket`][].
|
2019-10-01 17:57:09 +02:00
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* {stream.Duplex}
|
2019-08-06 13:56:52 +02:00
|
|
|
|
2017-06-11 20:18:03 +02:00
|
|
|
See [`response.socket`][].
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.cork()`
|
2019-08-08 21:12:41 +02:00
|
|
|
<!-- YAML
|
2019-11-19 18:41:01 +01:00
|
|
|
added: v13.2.0
|
2019-08-08 21:12:41 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
See [`writable.cork()`][].
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.end([data[, encoding]][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
2018-02-14 13:32:01 +01:00
|
|
|
changes:
|
2018-03-02 18:53:46 +01:00
|
|
|
- version: v10.0.0
|
2018-02-14 13:32:01 +01: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-26 01:04:07 +02:00
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
* `data` {string|Buffer}
|
2017-02-04 16:15:33 +01:00
|
|
|
* `encoding` {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
2018-02-14 13:32:01 +01:00
|
|
|
* Returns: {this}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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-28 00:30:32 +01:00
|
|
|
The method, `response.end()`, MUST be called on each response.
|
2011-04-26 01:04:07 +02:00
|
|
|
|
2019-03-06 10:26:33 +01:00
|
|
|
If `data` is specified, it is similar in effect to calling
|
2015-11-28 00:30:32 +01:00
|
|
|
[`response.write(data, encoding)`][] followed by `response.end(callback)`.
|
2011-07-26 00:13:24 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 12:53:13 +01:00
|
|
|
### `response.finished`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.2
|
2019-12-13 17:02:36 +01:00
|
|
|
deprecated: v13.4.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-04 00:45:20 +01:00
|
|
|
* {boolean}
|
2016-09-11 01:03:30 +02: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 12:53:13 +01:00
|
|
|
### `response.flushHeaders()`
|
2019-07-22 17:30:20 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.6.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
Flushes the response headers. See also: [`request.flushHeaders()`][].
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.getHeader(name)`
|
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-11 01:03:30 +02:00
|
|
|
|
2017-01-15 13:46:53 +01:00
|
|
|
Reads out a header that's already been queued but not sent to the client.
|
2019-06-20 21:52:54 +02: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 20:39:42 +02: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 12:53:13 +01:00
|
|
|
### `response.getHeaderNames()`
|
2017-01-14 15:39:58 +01:00
|
|
|
<!-- YAML
|
2017-02-28 04:49:09 +01:00
|
|
|
added: v7.7.0
|
2017-01-14 15:39:58 +01:00
|
|
|
-->
|
|
|
|
|
2018-04-09 14:25:04 +02:00
|
|
|
* Returns: {string[]}
|
2017-01-14 15:39:58 +01: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 20:39:42 +02:00
|
|
|
const headerNames = response.getHeaderNames();
|
2017-01-14 15:39:58 +01:00
|
|
|
// headerNames === ['foo', 'set-cookie']
|
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.getHeaders()`
|
2017-01-14 15:39:58 +01:00
|
|
|
<!-- YAML
|
2017-02-28 04:49:09 +01:00
|
|
|
added: v7.7.0
|
2017-01-14 15:39:58 +01: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-06 06:55:16 +01:00
|
|
|
The object returned by the `response.getHeaders()` method _does not_
|
2017-05-07 20:50:10 +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*.
|
|
|
|
|
2017-01-14 15:39:58 +01:00
|
|
|
```js
|
|
|
|
response.setHeader('Foo', 'bar');
|
|
|
|
response.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);
|
|
|
|
|
2017-04-02 20:39:42 +02:00
|
|
|
const headers = response.getHeaders();
|
2017-01-14 15:39:58 +01:00
|
|
|
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
|
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.hasHeader(name)`
|
2017-01-14 15:39:58 +01:00
|
|
|
<!-- YAML
|
2017-02-28 04:49:09 +01:00
|
|
|
added: v7.7.0
|
2017-01-14 15:39:58 +01:00
|
|
|
-->
|
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* `name` {string}
|
|
|
|
* Returns: {boolean}
|
2017-01-14 15:39:58 +01:00
|
|
|
|
|
|
|
Returns `true` if the header identified by `name` is currently set in the
|
2019-06-20 21:52:54 +02:00
|
|
|
outgoing headers. The header name matching is case-insensitive.
|
2017-01-14 15:39:58 +01:00
|
|
|
|
|
|
|
```js
|
2017-04-02 20:39:42 +02:00
|
|
|
const hasContentType = response.hasHeader('content-type');
|
2017-01-14 15:39:58 +01:00
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.headersSent`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
|
|
|
-->
|
2012-01-09 03:51:06 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {boolean}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Boolean (read-only). True if headers were sent, false otherwise.
|
2012-01-09 03:51:06 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.removeHeader(name)`
|
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-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.sendDate`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.5
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {boolean}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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-20 01:50:12 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
This should only be disabled for testing; HTTP requires the Date header
|
|
|
|
in responses.
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.setHeader(name, value)`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.4.0
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `name` {string}
|
2018-04-09 22:43:37 +02:00
|
|
|
* `value` {any}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2018-04-02 07:38:48 +02: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
|
|
|
|
for network transmission.
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
response.setHeader('Content-Type', 'text/html');
|
|
|
|
```
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
or
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
|
|
|
|
```
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2016-02-04 02:32:23 +01:00
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
|
|
|
will result in a [`TypeError`][] being thrown.
|
2011-05-20 01:50:12 +02:00
|
|
|
|
2018-02-12 08:31:55 +01: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 14:48:41 +01:00
|
|
|
|
|
|
|
```js
|
2019-03-07 01:03:53 +01:00
|
|
|
// Returns content-type = text/plain
|
2017-04-02 20:39:42 +02:00
|
|
|
const server = http.createServer((req, res) => {
|
2016-02-05 14:48:41 +01:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.setHeader('X-Foo', 'bar');
|
2017-06-01 01:07:25 +02:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-02-05 14:48:41 +01:00
|
|
|
res.end('ok');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2018-06-12 16:41:07 +02: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 12:53:13 +01:00
|
|
|
### `response.setTimeout(msecs[, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.12
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `msecs` {number}
|
2015-11-04 23:56:03 +01:00
|
|
|
* `callback` {Function}
|
2018-04-11 20:07:14 +02:00
|
|
|
* Returns: {http.ServerResponse}
|
2011-06-11 15:09:40 +02:00
|
|
|
|
2018-04-02 07:38:48 +02:00
|
|
|
Sets the Socket's timeout value to `msecs`. If a callback is
|
2015-11-04 23:56:03 +01:00
|
|
|
provided, then it is added as a listener on the `'timeout'` event on
|
|
|
|
the response object.
|
2011-06-11 15:09:40 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
If no `'timeout'` listener is added to the request, the response, or
|
2018-04-02 07:38:48 +02:00
|
|
|
the server, then sockets are destroyed when they time out. If a handler is
|
2017-03-04 00:45:20 +01:00
|
|
|
assigned to the request, the response, or the server's `'timeout'` events,
|
|
|
|
timed out sockets must be handled explicitly.
|
2011-06-11 15:09:40 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.socket`
|
2017-06-11 20:18:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* {stream.Duplex}
|
2017-06-11 20:18:03 +02: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
|
|
|
|
`response.end()`, the property is nulled. The `socket` may also be accessed
|
|
|
|
via `response.connection`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const http = require('http');
|
|
|
|
const server = http.createServer((req, res) => {
|
2017-08-04 11:56:03 +02:00
|
|
|
const ip = res.socket.remoteAddress;
|
|
|
|
const port = res.socket.remotePort;
|
2017-06-11 20:18:03 +02: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 12:53:13 +01:00
|
|
|
### `response.statusCode`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.4.0
|
|
|
|
-->
|
2015-02-25 01:11:11 +01:00
|
|
|
|
2019-08-05 19:03:42 +02:00
|
|
|
* {number} **Default:** `200`
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
When using implicit headers (not calling [`response.writeHead()`][] explicitly),
|
2015-11-04 23:56:03 +01:00
|
|
|
this property controls the status code that will be sent to the client when
|
|
|
|
the headers get flushed.
|
2015-02-25 01:11:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
response.statusCode = 404;
|
|
|
|
```
|
2014-04-09 15:02:03 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 12:53:13 +01:00
|
|
|
### `response.statusMessage`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.8
|
|
|
|
-->
|
2014-04-09 15:02:03 +02:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2018-02-12 08:31:55 +01: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 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
response.statusMessage = 'Not found';
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
After response header was sent to the client, this property indicates the
|
|
|
|
status message which was sent out.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.uncork()`
|
2019-08-08 21:12:41 +02:00
|
|
|
<!-- YAML
|
2019-11-19 18:41:01 +01:00
|
|
|
added: v13.2.0
|
2019-08-08 21:12:41 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
See [`writable.uncork()`][].
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.writableEnded`
|
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 12:53:13 +01:00
|
|
|
### `response.writableFinished`
|
2019-08-02 18:39:49 +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 12:53:13 +01:00
|
|
|
### `response.write(chunk[, encoding][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.29
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
* `chunk` {string|Buffer}
|
2018-04-02 03:44:32 +02:00
|
|
|
* `encoding` {string} **Default:** `'utf8'`
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
2017-03-04 00:45:20 +01:00
|
|
|
* Returns: {boolean}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
If this method is called and [`response.writeHead()`][] has not been called,
|
2015-11-04 23:56:03 +01:00
|
|
|
it will switch to implicit header mode and flush the implicit headers.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
This sends a chunk of the response body. This method may
|
|
|
|
be called multiple times to provide successive parts of the body.
|
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
In the `http` module, the response body is omitted when the
|
2017-04-10 21:57:12 +02:00
|
|
|
request is a HEAD request. Similarly, the `204` and `304` responses
|
|
|
|
_must not_ include a message body.
|
|
|
|
|
2015-11-04 23:56:03 +01: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 03:44:32 +02:00
|
|
|
`callback` will be called when this chunk of data is flushed.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2018-02-06 06:55:16 +01: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 14:18:16 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The first time [`response.write()`][] is called, it will send the buffered
|
2017-03-04 00:45:20 +01: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 08:52:42 +01:00
|
|
|
|
2015-11-04 23:56:03 +01: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-05 00:14:58 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.writeContinue()`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2011-02-05 00:14:58 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Sends a HTTP/1.1 100 Continue message to the client, indicating that
|
2018-02-12 08:31:55 +01:00
|
|
|
the request body should be sent. See the [`'checkContinue'`][] event on
|
|
|
|
`Server`.
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.writeHead(statusCode[, statusMessage][, headers])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.30
|
2017-02-21 23:38:45 +01:00
|
|
|
changes:
|
2019-02-14 14:37:22 +01:00
|
|
|
- version: v11.10.0
|
2019-02-06 23:17:23 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/25974
|
|
|
|
description: Return `this` from `writeHead()` to allow chaining with
|
|
|
|
`end()`.
|
2017-02-21 23:38:45 +01:00
|
|
|
- version: v5.11.0, v4.4.5
|
|
|
|
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 23:31:17 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `statusCode` {number}
|
|
|
|
* `statusMessage` {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
* `headers` {Object}
|
2019-02-06 23:17:23 +01:00
|
|
|
* Returns: {http.ServerResponse}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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-14 04:25:57 +02:00
|
|
|
|
2019-02-06 23:17:23 +01: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 20:39:42 +02:00
|
|
|
const body = 'hello world';
|
2019-02-06 23:17:23 +01:00
|
|
|
response
|
|
|
|
.writeHead(200, {
|
|
|
|
'Content-Length': Buffer.byteLength(body),
|
|
|
|
'Content-Type': 'text/plain'
|
|
|
|
})
|
|
|
|
.end(body);
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
This method must only be called once on a message and it must
|
2015-11-28 00:30:32 +01:00
|
|
|
be called before [`response.end()`][] is called.
|
2011-08-12 23:31:17 +02:00
|
|
|
|
2017-03-04 00:45:20 +01: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 14:18:16 +02:00
|
|
|
|
2018-02-12 08:31:55 +01: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 14:48:41 +01:00
|
|
|
|
2018-06-12 16:41:07 +02: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 14:48:41 +01:00
|
|
|
```js
|
2019-03-07 01:03:53 +01:00
|
|
|
// Returns content-type = text/plain
|
2017-04-02 20:39:42 +02:00
|
|
|
const server = http.createServer((req, res) => {
|
2016-02-05 14:48:41 +01:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
|
|
|
res.setHeader('X-Foo', 'bar');
|
2017-06-01 01:07:25 +02:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2016-02-05 14:48:41 +01:00
|
|
|
res.end('ok');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
`Content-Length` is given in bytes not characters. The above example
|
2015-11-04 23:56:03 +01:00
|
|
|
works because the string `'hello world'` contains only single byte characters.
|
|
|
|
If the body contains higher coded characters then `Buffer.byteLength()`
|
|
|
|
should be used to determine the number of bytes in a given encoding.
|
2019-06-20 21:52:54 +02:00
|
|
|
And Node.js does not check whether `Content-Length` and the length of the body
|
2015-11-04 23:56:03 +01:00
|
|
|
which has been transmitted are equal or not.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-02-04 02:32:23 +01:00
|
|
|
Attempting to set a header field name or value that contains invalid characters
|
|
|
|
will result in a [`TypeError`][] being thrown.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `response.writeProcessing()`
|
2018-01-08 09:11:05 +01:00
|
|
|
<!-- YAML
|
2018-03-02 18:53:46 +01:00
|
|
|
added: v10.0.0
|
2018-01-08 09:11:05 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
Sends a HTTP/1.1 102 Processing message to the client, indicating that
|
|
|
|
the request body should be sent.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## Class: `http.IncomingMessage`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.17
|
2019-10-26 03:17:06 +02:00
|
|
|
changes:
|
2019-11-05 10:55:13 +01:00
|
|
|
- version: v13.1.0
|
2019-10-26 03:17:06 +02: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 14:18:16 +02:00
|
|
|
|
2019-08-21 22:11:16 +02:00
|
|
|
* Extends: {stream.Readable}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
An `IncomingMessage` object is created by [`http.Server`][] or
|
2016-09-11 01:03:30 +02:00
|
|
|
[`http.ClientRequest`][] and passed as the first argument to the [`'request'`][]
|
2018-02-12 08:31:55 +01:00
|
|
|
and [`'response'`][] event respectively. It may be used to access response
|
|
|
|
status, headers and data.
|
2012-02-05 11:11:54 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'aborted'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.8
|
|
|
|
-->
|
2016-06-11 00:41:36 +02:00
|
|
|
|
2018-04-17 11:37:50 +02:00
|
|
|
Emitted when the request has been aborted.
|
2016-06-11 00:41:36 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### Event: `'close'`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.4.2
|
|
|
|
-->
|
2011-06-11 15:09:40 +02:00
|
|
|
|
2013-02-15 23:21:48 +01:00
|
|
|
Indicates that the underlying connection was closed.
|
2012-10-12 15:27:47 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.aborted`
|
2018-04-17 11:37:50 +02:00
|
|
|
<!-- YAML
|
2018-05-08 18:11:24 +02:00
|
|
|
added: v10.1.0
|
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 12:53:13 +01:00
|
|
|
### `message.complete`
|
2018-10-27 00:09:16 +02: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,
|
|
|
|
method: 'POST'
|
|
|
|
}, (res) => {
|
|
|
|
res.resume();
|
|
|
|
res.on('end', () => {
|
|
|
|
if (!res.complete)
|
|
|
|
console.error(
|
|
|
|
'The connection was terminated while the message was still being sent');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.destroy([error])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2016-06-09 01:23:21 +02:00
|
|
|
|
|
|
|
* `error` {Error}
|
|
|
|
|
|
|
|
Calls `destroy()` on the socket that received the `IncomingMessage`. If `error`
|
2019-11-04 23:45:27 +01: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-09 01:23:21 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.headers`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.5
|
|
|
|
-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* {Object}
|
|
|
|
|
2013-01-20 00:49:30 +01:00
|
|
|
The request/response headers object.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-13 16:01:34 +01:00
|
|
|
Key-value pairs of header names and values. Header names are lower-cased.
|
2010-10-28 14:18:16 +02: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: '*/*' }
|
|
|
|
console.log(request.headers);
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-13 08:21:22 +01:00
|
|
|
Duplicates in raw headers are handled in the following ways, depending on the
|
|
|
|
header name:
|
|
|
|
|
|
|
|
* Duplicates of `age`, `authorization`, `content-length`, `content-type`,
|
|
|
|
`etag`, `expires`, `from`, `host`, `if-modified-since`, `if-unmodified-since`,
|
|
|
|
`last-modified`, `location`, `max-forwards`, `proxy-authorization`, `referer`,
|
2019-10-14 05:18:21 +02:00
|
|
|
`retry-after`, `server`, or `user-agent` are discarded.
|
2015-11-13 08:21:22 +01:00
|
|
|
* `set-cookie` is always an array. Duplicates are added to the array.
|
2018-11-30 09:53:57 +01:00
|
|
|
* For duplicate `cookie` headers, the values are joined together with '; '.
|
2015-11-13 08:21:22 +01:00
|
|
|
* For all other headers, the values are joined together with ', '.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.httpVersion`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.1
|
|
|
|
-->
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-04 23:56:03 +01: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 10:15:28 +01:00
|
|
|
Also `message.httpVersionMajor` is the first integer and
|
|
|
|
`message.httpVersionMinor` is the second.
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.method`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.1
|
|
|
|
-->
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
**Only valid for request obtained from [`http.Server`][].**
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
The request method as a string. Read only. Examples: `'GET'`, `'DELETE'`.
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.rawHeaders`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.6
|
|
|
|
-->
|
2013-08-15 23:12:12 +02:00
|
|
|
|
2018-04-09 14:25:04 +02:00
|
|
|
* {string[]}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
The raw request/response headers list exactly as they were received.
|
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
The keys and values are in the same list. It is *not* a
|
2018-04-02 07:38:48 +02:00
|
|
|
list of tuples. So, the even-numbered offsets are key values, and the
|
2013-08-15 23:12:12 +02: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 23:12:12 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.rawTrailers`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.6
|
|
|
|
-->
|
2013-08-15 23:12:12 +02:00
|
|
|
|
2018-04-09 14:25:04 +02:00
|
|
|
* {string[]}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2013-08-15 23:12:12 +02:00
|
|
|
The raw request/response trailer keys and values exactly as they were
|
2018-04-02 07:38:48 +02:00
|
|
|
received. Only populated at the `'end'` event.
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.setTimeout(msecs[, callback])`
|
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-04 08:29:22 +01: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-04 08:29:22 +01:00
|
|
|
* `callback` {Function}
|
2018-04-11 20:07:14 +02: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-04 08:29:22 +01:00
|
|
|
|
|
|
|
Calls `message.connection.setTimeout(msecs, callback)`.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.socket`
|
2017-02-13 03:49:35 +01:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
|
|
|
|
2019-10-28 16:47:07 +01:00
|
|
|
* {stream.Duplex}
|
2017-02-13 03:49:35 +01: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
|
|
|
|
type other than {net.Socket}.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.statusCode`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.1
|
|
|
|
-->
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {number}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
**Only valid for response obtained from [`http.ClientRequest`][].**
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
The 3-digit HTTP response status code. E.G. `404`.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.statusMessage`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.10
|
|
|
|
-->
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
**Only valid for response obtained from [`http.ClientRequest`][].**
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2018-02-12 08:31:55 +01:00
|
|
|
The HTTP response status message (reason phrase). E.G. `OK` or `Internal Server
|
|
|
|
Error`.
|
2016-01-22 23:45:12 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.trailers`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* {Object}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The request/response trailers object. Only populated at the `'end'` event.
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
### `message.url`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.90
|
|
|
|
-->
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2017-03-04 00:45:20 +01:00
|
|
|
* {string}
|
2016-09-11 01:03:30 +02:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
**Only valid for request obtained from [`http.Server`][].**
|
2013-01-20 00:49:30 +01:00
|
|
|
|
|
|
|
Request URL string. This contains only the URL that is
|
|
|
|
present in the actual HTTP request. If the request is:
|
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
```txt
|
2016-01-17 18:39:07 +01:00
|
|
|
GET /status?name=ryan HTTP/1.1\r\n
|
|
|
|
Accept: text/plain\r\n
|
|
|
|
\r\n
|
|
|
|
```
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2019-12-07 05:20:20 +01:00
|
|
|
To parse the URL into its parts:
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
```js
|
2019-12-07 05:20:20 +01:00
|
|
|
new URL(request.url, `http://${request.headers.host}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2019-12-07 05:20:20 +01:00
|
|
|
When `request.url` is `'/status?name=ryan'` and
|
|
|
|
`request.headers.host` is `'localhost:3000'`:
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2019-09-01 05:07:24 +02:00
|
|
|
```console
|
2016-01-21 23:21:22 +01:00
|
|
|
$ node
|
2020-01-10 19:06:18 +01:00
|
|
|
> new URL(request.url, `http://${request.headers.host}`)
|
2019-12-07 05:20:20 +01:00
|
|
|
URL {
|
|
|
|
href: 'http://localhost:3000/status?name=ryan',
|
|
|
|
origin: 'http://localhost:3000',
|
|
|
|
protocol: 'http:',
|
|
|
|
username: '',
|
|
|
|
password: '',
|
|
|
|
host: 'localhost:3000',
|
|
|
|
hostname: 'localhost',
|
|
|
|
port: '3000',
|
2017-04-02 20:39:42 +02:00
|
|
|
pathname: '/status',
|
2016-01-17 18:39:07 +01:00
|
|
|
search: '?name=ryan',
|
2019-12-07 05:20:20 +01:00
|
|
|
searchParams: URLSearchParams { 'name' => 'ryan' },
|
|
|
|
hash: ''
|
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## `http.METHODS`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.8
|
|
|
|
-->
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2018-04-09 14:25:04 +02:00
|
|
|
* {string[]}
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
A list of the HTTP methods that are supported by the parser.
|
2013-01-20 00:49:30 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## `http.STATUS_CODES`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.22
|
|
|
|
-->
|
2013-09-22 16:06:58 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
* {Object}
|
2013-09-22 16:06:58 +02:00
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
A collection of all the standard HTTP response status codes, and the
|
2018-04-02 07:38:48 +02:00
|
|
|
short description of each. For example, `http.STATUS_CODES[404] === 'Not
|
2015-11-04 23:56:03 +01:00
|
|
|
Found'`.
|
2013-09-22 16:06:58 +02:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## `http.createServer([options][, requestListener])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.13
|
2018-02-06 22:34:50 +01:00
|
|
|
changes:
|
2020-02-04 18:58:51 +01:00
|
|
|
- version: v13.8.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-12-12 23:02:35 +01: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 07:42:22 +02:00
|
|
|
|
2018-07-12 19:48:11 +02:00
|
|
|
* `options` {Object}
|
2018-04-29 19:46:41 +02:00
|
|
|
* `IncomingMessage` {http.IncomingMessage} Specifies the `IncomingMessage`
|
|
|
|
class to be used. Useful for extending the original `IncomingMessage`.
|
2018-04-02 03:44:32 +02:00
|
|
|
**Default:** `IncomingMessage`.
|
2018-04-29 19:46:41 +02:00
|
|
|
* `ServerResponse` {http.ServerResponse} Specifies the `ServerResponse` class
|
|
|
|
to be used. Useful for extending the original `ServerResponse`. **Default:**
|
2018-04-02 03:44:32 +02:00
|
|
|
`ServerResponse`.
|
2020-01-21 22:35:27 +01:00
|
|
|
* `insecureHTTPParser` {boolean} Use an insecure HTTP parser that accepts
|
|
|
|
invalid HTTP headers when `true`. Using the insecure parser should be
|
|
|
|
avoided. See [`--insecure-http-parser`][] for more information.
|
|
|
|
**Default:** `false`
|
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.
|
2020-03-27 16:34:24 +01:00
|
|
|
**Default:** 16384 (16KB).
|
2018-07-12 19:48:11 +02:00
|
|
|
* `requestListener` {Function}
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* Returns: {http.Server}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Returns a new instance of [`http.Server`][].
|
2015-11-04 23:56:03 +01:00
|
|
|
|
|
|
|
The `requestListener` is a function which is automatically
|
2016-09-11 01:03:30 +02:00
|
|
|
added to the [`'request'`][] event.
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## `http.get(options[, callback])`
|
|
|
|
## `http.get(url[, options][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.6
|
2017-06-02 17:07:06 +02:00
|
|
|
changes:
|
2018-08-13 11:02:06 +02:00
|
|
|
- version: v10.9.0
|
2018-07-01 17:00:24 +02: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 17:07:06 +02: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 23:56:03 +01:00
|
|
|
|
2018-07-01 17:00:24 +02:00
|
|
|
* `url` {string | URL}
|
|
|
|
* `options` {Object} Accepts the same `options` as
|
2017-03-04 00:45:20 +01:00
|
|
|
[`http.request()`][], with the `method` always set to `GET`.
|
2017-03-29 22:10:42 +02:00
|
|
|
Properties that are inherited from the prototype are ignored.
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
|
|
|
* Returns: {http.ClientRequest}
|
|
|
|
|
2015-11-04 23:56:03 +01: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
|
|
|
|
[`http.request()`][] is that it sets the method to GET and calls `req.end()`
|
2019-06-20 21:52:54 +02:00
|
|
|
automatically. The callback must take care to consume the response
|
2017-08-27 00:20:34 +02:00
|
|
|
data for reasons stated in [`http.ClientRequest`][] section.
|
2015-11-04 23:56:03 +01: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 13:16:44 +02:00
|
|
|
[`http.IncomingMessage`][].
|
2016-10-12 21:57:04 +02:00
|
|
|
|
2018-08-26 18:02:27 +02:00
|
|
|
JSON fetching example:
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2016-10-12 21:57:04 +02:00
|
|
|
http.get('http://nodejs.org/dist/index.json', (res) => {
|
2017-04-02 20:39:42 +02:00
|
|
|
const { statusCode } = res;
|
2016-10-12 21:57:04 +02:00
|
|
|
const contentType = res.headers['content-type'];
|
|
|
|
|
|
|
|
let error;
|
|
|
|
if (statusCode !== 200) {
|
2017-04-21 16:38:31 +02: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 16:38:31 +02: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 20:39:42 +02: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 20:39:42 +02:00
|
|
|
res.on('data', (chunk) => { rawData += chunk; });
|
2016-10-12 21:57:04 +02:00
|
|
|
res.on('end', () => {
|
|
|
|
try {
|
2017-04-02 20:39:42 +02:00
|
|
|
const parsedData = JSON.parse(rawData);
|
2016-10-12 21:57:04 +02:00
|
|
|
console.log(parsedData);
|
|
|
|
} catch (e) {
|
2017-04-02 20:39:42 +02: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 20:39:42 +02:00
|
|
|
console.error(`Got error: ${e.message}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
```
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## `http.globalAgent`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.9
|
|
|
|
-->
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2016-09-11 01:03:30 +02:00
|
|
|
* {http.Agent}
|
|
|
|
|
2017-01-09 18:45:46 +01:00
|
|
|
Global instance of `Agent` which is used as the default for all HTTP client
|
2015-11-04 23:56:03 +01:00
|
|
|
requests.
|
|
|
|
|
2019-12-24 12:53:13 +01:00
|
|
|
## `http.maxHeaderSize`
|
2018-12-06 01:59:12 +01:00
|
|
|
<!-- YAML
|
2018-12-22 00:13:09 +01:00
|
|
|
added: v11.6.0
|
2018-12-06 01:59:12 +01:00
|
|
|
-->
|
|
|
|
|
|
|
|
* {number}
|
|
|
|
|
|
|
|
Read-only property specifying the maximum allowed size of HTTP headers in bytes.
|
|
|
|
Defaults to 8KB. Configurable using the [`--max-http-header-size`][] CLI option.
|
|
|
|
|
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 12:53:13 +01:00
|
|
|
## `http.request(options[, callback])`
|
|
|
|
## `http.request(url[, options][, callback])`
|
2016-06-23 21:50:36 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.6
|
2017-06-02 17:07:06 +02:00
|
|
|
changes:
|
2020-02-04 18:58:51 +01:00
|
|
|
- version: v13.8.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 11:02:06 +02:00
|
|
|
- version: v10.9.0
|
2018-07-01 17:00:24 +02: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 17:07:06 +02: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 23:56:03 +01:00
|
|
|
|
2018-07-01 17:00:24 +02: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.
|
|
|
|
* `auth` {string} Basic authentication i.e. `'user:password'` to compute an
|
|
|
|
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.
|
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 13:22:14 +02: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 18:58:42 +01:00
|
|
|
`hostname` will be used if both `host` and `hostname` are specified.
|
2020-01-21 22:35:27 +01:00
|
|
|
* `insecureHTTPParser` {boolean} Use an insecure HTTP parser that accepts
|
|
|
|
invalid HTTP headers when `true`. Using the insecure parser should be
|
|
|
|
avoided. See [`--insecure-http-parser`][] for more information.
|
|
|
|
**Default:** `false`
|
2017-02-04 16:15:33 +01:00
|
|
|
* `localAddress` {string} Local interface to bind for network connections.
|
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
|
|
|
|
[`--max-http-header-size`][] for requests received from the server, i.e.
|
|
|
|
the maximum length of response headers in bytes.
|
2020-03-27 16:34:24 +01:00
|
|
|
**Default:** 16384 (16KB).
|
2018-04-02 03:44:32 +02:00
|
|
|
* `method` {string} A string specifying the HTTP request method. **Default:**
|
2016-11-13 15:43:55 +01:00
|
|
|
`'GET'`.
|
2018-04-02 03:44:32 +02: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:'`.
|
2018-03-21 05:11:40 +01:00
|
|
|
* `setHost` {boolean}: Specifies whether or not to automatically add the
|
|
|
|
`Host` header. Defaults to `true`.
|
2019-02-16 19:47:27 +01:00
|
|
|
* `socketPath` {string} Unix Domain Socket (cannot be used if one of `host`
|
|
|
|
or `port` is specified, those specify a TCP Socket).
|
|
|
|
* `timeout` {number}: A number specifying the socket timeout in milliseconds.
|
|
|
|
This will set the timeout before the socket is connected.
|
2016-09-11 01:03:30 +02:00
|
|
|
* `callback` {Function}
|
|
|
|
* Returns: {http.ClientRequest}
|
|
|
|
|
2015-11-04 23:56:03 +01:00
|
|
|
Node.js maintains several connections per server to make HTTP requests.
|
|
|
|
This function allows one to transparently issue requests.
|
|
|
|
|
2018-07-01 17:00:24 +02:00
|
|
|
`url` can be a string or a [`URL`][] object. If `url` is a
|
2018-04-25 02:37:43 +02:00
|
|
|
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
|
2017-06-02 17:07:06 +02:00
|
|
|
object, it will be automatically converted to an ordinary `options` object.
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2018-07-01 17:00:24 +02:00
|
|
|
If both `url` and `options` are specified, the objects are merged, with the
|
|
|
|
`options` properties taking precedence.
|
|
|
|
|
2017-12-06 19:15:58 +01:00
|
|
|
The optional `callback` parameter will be added as a one-time listener for
|
2016-07-09 07:13:09 +02:00
|
|
|
the [`'response'`][] event.
|
2015-11-04 23:56:03 +01:00
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
`http.request()` returns an instance of the [`http.ClientRequest`][]
|
2015-11-04 23:56:03 +01: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.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-02 20:39:42 +02:00
|
|
|
const postData = querystring.stringify({
|
|
|
|
'msg': 'Hello World!'
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
|
2017-04-02 20:39:42 +02:00
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
hostname: 'www.google.com',
|
|
|
|
port: 80,
|
|
|
|
path: '/upload',
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
2016-06-12 06:44:25 +02:00
|
|
|
'Content-Length': Buffer.byteLength(postData)
|
2016-01-17 18:39:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-02 20:39:42 +02: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-15 07:41:29 +02:00
|
|
|
console.log('No more data in response.');
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
2017-04-02 20:39:42 +02: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 23:56:03 +01:00
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
In the example `req.end()` was called. With `http.request()` one
|
2017-03-04 00:45:20 +01:00
|
|
|
must always call `req.end()` to signify the end of the request -
|
2015-11-04 23:56:03 +01: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 19:25:10 +01:00
|
|
|
on the returned request object. As with all `'error'` events, if no listeners
|
|
|
|
are registered the error will be thrown.
|
2015-11-04 23:56:03 +01: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 11:11:17 +01:00
|
|
|
* Sending a 'Content-Length' header will disable the default chunked encoding.
|
2015-11-04 23:56:03 +01:00
|
|
|
|
|
|
|
* Sending an 'Expect' header will immediately send the request headers.
|
2017-03-04 00:45:20 +01:00
|
|
|
Usually, when sending 'Expect: 100-continue', both a timeout and a listener
|
2019-03-18 05:32:12 +01:00
|
|
|
for the `'continue'` event should be set. See RFC 2616 Section 8.2.3 for more
|
2015-11-04 23:56:03 +01:00
|
|
|
information.
|
|
|
|
|
|
|
|
* Sending an Authorization header will override using the `auth` option
|
|
|
|
to compute basic authentication.
|
2012-06-06 21:05:18 +02:00
|
|
|
|
2017-06-02 17:07:06 +02: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-08 01:43:39 +01:00
|
|
|
In a successful request, the following events will be emitted in the following
|
|
|
|
order:
|
|
|
|
|
2018-04-09 18:30:22 +02: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-08 01:43:39 +01:00
|
|
|
instance, in most redirects)
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'end'` on the `res` object
|
|
|
|
* `'close'`
|
2018-01-08 01:43:39 +01:00
|
|
|
|
|
|
|
In the case of a connection error, the following events will be emitted:
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'socket'`
|
|
|
|
* `'error'`
|
|
|
|
* `'close'`
|
2018-01-08 01:43:39 +01: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
|
|
|
|
* `'close'`
|
|
|
|
* `'close'` on the `res` object
|
|
|
|
|
2018-01-08 01:43:39 +01:00
|
|
|
If `req.abort()` is called before the connection succeeds, the following events
|
|
|
|
will be emitted in the following order:
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'socket'`
|
2018-01-08 01:43:39 +01:00
|
|
|
* (`req.abort()` called here)
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'abort'`
|
2019-07-15 16:57:40 +02: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-08 01:43:39 +01:00
|
|
|
|
|
|
|
If `req.abort()` is called after the response is received, the following events
|
|
|
|
will be emitted in the following order:
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'socket'`
|
|
|
|
* `'response'`
|
|
|
|
* `'data'` any number of times, on the `res` object
|
2018-01-08 01:43:39 +01:00
|
|
|
* (`req.abort()` called here)
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'abort'`
|
2018-12-02 09:24:47 +01:00
|
|
|
* `'aborted'` on the `res` object
|
2018-04-09 18:30:22 +02:00
|
|
|
* `'close'`
|
2018-12-02 09:24:47 +01:00
|
|
|
* `'close'` on the `res` object
|
2018-04-09 18:30:22 +02:00
|
|
|
|
2019-06-20 21:52:54 +02:00
|
|
|
Setting the `timeout` option or using the `setTimeout()` function will
|
2018-04-09 18:30:22 +02:00
|
|
|
not abort the request or do anything besides add a `'timeout'` event.
|
2018-01-08 01:43:39 +01:00
|
|
|
|
2020-01-21 22:35:27 +01:00
|
|
|
[`--insecure-http-parser`]: cli.html#cli_insecure_http_parser
|
2018-12-06 01:59:12 +01:00
|
|
|
[`--max-http-header-size`]: cli.html#cli_max_http_header_size_size
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'checkContinue'`]: #http_event_checkcontinue
|
2016-09-11 01:03:30 +02:00
|
|
|
[`'request'`]: #http_event_request
|
2015-11-28 00:30:32 +01:00
|
|
|
[`'response'`]: #http_event_response
|
2018-01-08 09:11:05 +01:00
|
|
|
[`'upgrade'`]: #http_event_upgrade
|
2015-11-28 00:30:32 +01:00
|
|
|
[`Agent`]: #http_class_http_agent
|
2017-10-17 22:19:27 +02:00
|
|
|
[`Duplex`]: stream.html#stream_class_stream_duplex
|
2017-05-08 18:30:13 +02:00
|
|
|
[`TypeError`]: errors.html#errors_class_typeerror
|
2017-06-02 17:07:06 +02:00
|
|
|
[`URL`]: url.html#url_the_whatwg_url_api
|
2016-04-13 12:30:14 +02:00
|
|
|
[`agent.createConnection()`]: #http_agent_createconnection_options_callback
|
2017-08-04 11:56:03 +02:00
|
|
|
[`agent.getName()`]: #http_agent_getname_options
|
2015-11-14 04:21:49 +01:00
|
|
|
[`destroy()`]: #http_agent_destroy
|
2019-11-10 18:52:18 +01:00
|
|
|
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
|
2019-07-23 07:21:49 +02:00
|
|
|
[`'finish'`]: #http_event_finish
|
2017-09-17 22:02:00 +02:00
|
|
|
[`getHeader(name)`]: #http_request_getheader_name
|
2015-11-28 00:30:32 +01:00
|
|
|
[`http.Agent`]: #http_class_http_agent
|
|
|
|
[`http.ClientRequest`]: #http_class_http_clientrequest
|
2016-01-15 06:56:17 +01:00
|
|
|
[`http.IncomingMessage`]: #http_class_http_incomingmessage
|
2015-11-28 00:30:32 +01:00
|
|
|
[`http.Server`]: #http_class_http_server
|
2018-11-12 12:36:32 +01:00
|
|
|
[`http.get()`]: #http_http_get_options_callback
|
2017-05-08 18:30:13 +02:00
|
|
|
[`http.globalAgent`]: #http_http_globalagent
|
|
|
|
[`http.request()`]: #http_http_request_options_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
[`message.headers`]: #http_message_headers
|
|
|
|
[`net.Server.close()`]: net.html#net_server_close_callback
|
2017-05-08 18:30:13 +02:00
|
|
|
[`net.Server`]: net.html#net_class_net_server
|
2015-11-28 00:30:32 +01:00
|
|
|
[`net.Socket`]: net.html#net_class_net_socket
|
2017-05-08 18:30:13 +02:00
|
|
|
[`net.createConnection()`]: net.html#net_net_createconnection_options_connectlistener
|
2018-04-25 02:37:43 +02:00
|
|
|
[`new URL()`]: url.html#url_constructor_new_url_input_base
|
2017-09-17 22:02:00 +02:00
|
|
|
[`removeHeader(name)`]: #http_request_removeheader_name
|
2017-08-04 11:56:03 +02:00
|
|
|
[`request.end()`]: #http_request_end_data_encoding_callback
|
2019-07-22 17:30:20 +02:00
|
|
|
[`request.flushHeaders()`]: #http_request_flushheaders
|
2018-04-09 22:43:37 +02:00
|
|
|
[`request.getHeader()`]: #http_request_getheader_name
|
|
|
|
[`request.setHeader()`]: #http_request_setheader_name_value
|
2017-09-17 09:30:16 +02:00
|
|
|
[`request.setTimeout()`]: #http_request_settimeout_timeout_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
|
2018-11-27 20:49:21 +01:00
|
|
|
[`request.socket`]: #http_request_socket
|
2019-08-02 08:09:06 +02:00
|
|
|
[`request.writableFinished`]: #http_request_writablefinished
|
2019-07-14 16:59:25 +02:00
|
|
|
[`request.writableEnded`]: #http_request_writableended
|
2017-07-07 18:05:08 +02:00
|
|
|
[`request.write(data, encoding)`]: #http_request_write_chunk_encoding_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
[`response.end()`]: #http_response_end_data_encoding_callback
|
2018-04-09 22:43:37 +02:00
|
|
|
[`response.getHeader()`]: #http_response_getheader_name
|
2016-02-05 14:48:41 +01:00
|
|
|
[`response.setHeader()`]: #http_response_setheader_name_value
|
2017-06-11 20:18:03 +02:00
|
|
|
[`response.socket`]: #http_response_socket
|
2019-08-02 08:09:06 +02:00
|
|
|
[`response.writableFinished`]: #http_response_writablefinished
|
2019-07-14 16:59:25 +02:00
|
|
|
[`response.writableEnded`]: #http_response_writableended
|
2015-11-28 00:30:32 +01:00
|
|
|
[`response.write()`]: #http_response_write_chunk_encoding_callback
|
|
|
|
[`response.write(data, encoding)`]: #http_response_write_chunk_encoding_callback
|
|
|
|
[`response.writeContinue()`]: #http_response_writecontinue
|
|
|
|
[`response.writeHead()`]: #http_response_writehead_statuscode_statusmessage_headers
|
2017-10-06 20:50:47 +02:00
|
|
|
[`server.listen()`]: net.html#net_server_listen
|
2015-10-29 20:53:43 +01:00
|
|
|
[`server.timeout`]: #http_server_timeout
|
2017-09-17 22:02:00 +02:00
|
|
|
[`setHeader(name, value)`]: #http_request_setheader_name_value
|
2018-12-05 07:41:19 +01:00
|
|
|
[`socket.connect()`]: net.html#net_socket_connect_options_connectlistener
|
2015-11-28 00:30:32 +01:00
|
|
|
[`socket.setKeepAlive()`]: net.html#net_socket_setkeepalive_enable_initialdelay
|
|
|
|
[`socket.setNoDelay()`]: net.html#net_socket_setnodelay_nodelay
|
|
|
|
[`socket.setTimeout()`]: net.html#net_socket_settimeout_timeout_callback
|
2018-04-29 19:46:41 +02:00
|
|
|
[`socket.unref()`]: net.html#net_socket_unref
|
2016-06-23 21:54:34 +02:00
|
|
|
[`url.parse()`]: url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
|
2019-01-21 07:47:32 +01:00
|
|
|
[`HPE_HEADER_OVERFLOW`]: errors.html#errors_hpe_header_overflow
|
2019-08-08 21:12:41 +02:00
|
|
|
[`writable.cork()`]: stream.html#stream_writable_cork
|
|
|
|
[`writable.uncork()`]: stream.html#stream_writable_uncork
|