Skip to content

Latest commit

Β 

History

History
2040 lines (1577 loc) Β· 56.5 KB

File metadata and controls

2040 lines (1577 loc) Β· 56.5 KB
Β 
Feb 27, 2012
Feb 27, 2012
1
# HTTP
Oct 28, 2010
Oct 28, 2010
2
Aug 28, 2017
Aug 28, 2017
3
<!--introduced_in=v0.10.0-->
4
Aug 4, 2016
Aug 4, 2016
5
> Stability: 2 - Stable
Mar 4, 2012
Mar 4, 2012
6
Oct 28, 2010
Oct 28, 2010
7
To use the HTTP server and client one must `require('http')`.
8
Aug 23, 2015
Aug 23, 2015
9
The HTTP interfaces in Node.js are designed to support many features
Oct 28, 2010
Oct 28, 2010
10
of the protocol which have been traditionally difficult to use.
11
In particular, large, possibly chunk-encoded, messages. The interface is
12
careful to never buffer entire requests or responses--the
13
user is able to stream data.
14
15
HTTP message headers are represented by an object like this:
16
Jul 5, 2017
Jul 5, 2017
17
<!-- eslint-skip -->
Jul 14, 2016
Jul 14, 2016
18
```js
Jan 21, 2016
Jan 21, 2016
19
{ 'content-length': '123',
20
'content-type': 'text/plain',
21
'connection': 'keep-alive',
22
'host': 'mysite.com',
23
'accept': '*/*' }
24
```
Oct 28, 2010
Oct 28, 2010
25
26
Keys are lowercased. Values are not modified.
27
Aug 23, 2015
Aug 23, 2015
28
In order to support the full spectrum of possible HTTP applications, Node.js's
Oct 28, 2010
Oct 28, 2010
29
HTTP API is very low-level. It deals with stream handling and message
30
parsing only. It parses a message into headers and body but it does not
31
parse the actual headers or the body.
32
Dec 3, 2015
Dec 3, 2015
33
See [`message.headers`][] for details on how duplicate headers are handled.
Aug 15, 2013
Aug 15, 2013
34
35
The raw headers as they were received are retained in the `rawHeaders`
36
property, which is an array of `[key, value, key2, value2, ...]`. For
37
example, the previous message header object might have a `rawHeaders`
38
list like the following:
39
Apr 24, 2017
Apr 24, 2017
40
<!-- eslint-disable semi -->
Jul 14, 2016
Jul 14, 2016
41
```js
Jan 21, 2016
Jan 21, 2016
42
[ 'ConTent-Length', '123456',
43
'content-LENGTH', '123',
44
'content-type', 'text/plain',
45
'CONNECTION', 'keep-alive',
46
'Host', 'mysite.com',
47
'accepT', '*/*' ]
48
```
Oct 28, 2010
Oct 28, 2010
49
Nov 13, 2015
Nov 13, 2015
50
## Class: http.Agent
Jun 29, 2016
Jun 29, 2016
51
<!-- YAML
52
added: v0.3.4
53
-->
Oct 28, 2010
Oct 28, 2010
54
Jan 25, 2017
Jan 25, 2017
55
An `Agent` is responsible for managing connection persistence
56
and reuse for HTTP clients. It maintains a queue of pending requests
57
for a given host and port, reusing a single socket connection for each
58
until the queue is empty, at which time the socket is either destroyed
59
or put into a pool where it is kept to be used again for requests to the
60
same host and port. Whether it is destroyed or pooled depends on the
61
`keepAlive` [option](#http_new_agent_options).
62
63
Pooled connections have TCP Keep-Alive enabled for them, but servers may
64
still close idle connections, in which case they will be removed from the
65
pool and a new connection will be made when a new HTTP request is made for
66
that host and port. Servers may also refuse to allow multiple requests
67
over the same connection, in which case the connection will have to be
68
remade for every request and cannot be pooled. The `Agent` will still make
69
the requests to that server, but each one will occur over a new connection.
70
71
When a connection is closed by the client or the server, it is removed
72
from the pool. Any unused sockets in the pool will be unrefed so as not
73
to keep the Node.js process running when there are no outstanding requests.
74
(see [socket.unref()]).
75
76
It is good practice, to [`destroy()`][] an `Agent` instance when it is no
77
longer in use, because unused sockets consume OS resources.
78
Aug 18, 2017
Aug 18, 2017
79
Sockets are removed from an agent when the socket emits either
Mar 8, 2017
Mar 8, 2017
80
a `'close'` event or an `'agentRemove'` event. When intending to keep one
Aug 18, 2017
Aug 18, 2017
81
HTTP request open for a long time without keeping it in the agent, something
Mar 8, 2017
Mar 8, 2017
82
like the following may be done:
Oct 28, 2010
Oct 28, 2010
83
Jan 21, 2016
Jan 21, 2016
84
```js
85
http.get(options, (res) => {
86
// Do stuff
87
}).on('socket', (socket) => {
88
socket.emit('agentRemove');
89
});
90
```
Oct 28, 2010
Oct 28, 2010
91
Mar 8, 2017
Mar 8, 2017
92
An agent may also be used for an individual request. By providing
Jan 25, 2017
Jan 25, 2017
93
`{agent: false}` as an option to the `http.get()` or `http.request()`
94
functions, a one-time use `Agent` with default options will be used
95
for the client connection.
96
Nov 13, 2015
Nov 13, 2015
97
`agent:false`:
Oct 28, 2010
Oct 28, 2010
98
Jan 21, 2016
Jan 21, 2016
99
```js
100
http.get({
101
hostname: 'localhost',
102
port: 80,
103
path: '/',
104
agent: false // create a new agent just for this one request
105
}, (res) => {
106
// Do stuff with response
Jul 15, 2016
Jul 15, 2016
107
});
Jan 21, 2016
Jan 21, 2016
108
```
Oct 28, 2010
Oct 28, 2010
109
Nov 13, 2015
Nov 13, 2015
110
### new Agent([options])
Jun 29, 2016
Jun 29, 2016
111
<!-- YAML
112
added: v0.3.4
113
-->
Oct 28, 2010
Oct 28, 2010
114
Nov 13, 2015
Nov 13, 2015
115
* `options` {Object} Set of configurable options to set on the agent.
116
Can have the following fields:
Mar 2, 2017
Mar 2, 2017
117
* `keepAlive` {boolean} Keep sockets around even when there are no
Jan 25, 2017
Jan 25, 2017
118
outstanding requests, so they can be used for future requests without
Mar 8, 2017
Mar 8, 2017
119
having to reestablish a TCP connection. Defaults to `false`
120
* `keepAliveMsecs` {number} When using the `keepAlive` option, specifies
Feb 4, 2017
Feb 4, 2017
121
the [initial delay](net.html#net_socket_setkeepalive_enable_initialdelay)
Jan 25, 2017
Jan 25, 2017
122
for TCP Keep-Alive packets. Ignored when the
Mar 8, 2017
Mar 8, 2017
123
`keepAlive` option is `false` or `undefined`. Defaults to `1000`.
Mar 2, 2017
Mar 2, 2017
124
* `maxSockets` {number} Maximum number of sockets to allow per
Mar 8, 2017
Mar 8, 2017
125
host. Defaults to `Infinity`.
Mar 2, 2017
Mar 2, 2017
126
* `maxFreeSockets` {number} Maximum number of sockets to leave open
Nov 13, 2015
Nov 13, 2015
127
in a free state. Only relevant if `keepAlive` is set to `true`.
Mar 8, 2017
Mar 8, 2017
128
Defaults to `256`.
Oct 28, 2010
Oct 28, 2010
129
Dec 3, 2015
Dec 3, 2015
130
The default [`http.globalAgent`][] that is used by [`http.request()`][] has all
Nov 13, 2015
Nov 13, 2015
131
of these values set to their respective defaults.
Jun 13, 2012
Jun 13, 2012
132
Mar 8, 2017
Mar 8, 2017
133
To configure any of them, a custom [`http.Agent`][] instance must be created.
Jun 13, 2012
Jun 13, 2012
134
Jan 21, 2016
Jan 21, 2016
135
```js
Dec 17, 2015
Dec 17, 2015
136
const http = require('http');
Apr 4, 2017
Apr 4, 2017
137
const keepAliveAgent = new http.Agent({ keepAlive: true });
Nov 13, 2015
Nov 13, 2015
138
options.agent = keepAliveAgent;
139
http.request(options, onResponseCallback);
140
```
Jun 13, 2012
Jun 13, 2012
141
Feb 11, 2016
Feb 11, 2016
142
### agent.createConnection(options[, callback])
Jun 29, 2016
Jun 29, 2016
143
<!-- YAML
144
added: v0.11.4
145
-->
Feb 11, 2016
Feb 11, 2016
146
Oct 11, 2016
Oct 11, 2016
147
* `options` {Object} Options containing connection details. Check
148
[`net.createConnection()`][] for the format of the options
149
* `callback` {Function} Callback function that receives the created socket
150
* Returns: {net.Socket}
151
Feb 11, 2016
Feb 11, 2016
152
Produces a socket/stream to be used for HTTP requests.
153
154
By default, this function is the same as [`net.createConnection()`][]. However,
Jan 25, 2017
Jan 25, 2017
155
custom agents may override this method in case greater flexibility is desired.
Feb 11, 2016
Feb 11, 2016
156
157
A socket/stream can be supplied in one of two ways: by returning the
158
socket/stream from this function, or by passing the socket/stream to `callback`.
159
160
`callback` has a signature of `(err, stream)`.
161
Jun 7, 2017
Jun 7, 2017
162
### agent.keepSocketAlive(socket)
163
<!-- YAML
Nov 16, 2017
Nov 16, 2017
164
added: v8.1.0
Jun 7, 2017
Jun 7, 2017
165
-->
166
167
* `socket` {net.Socket}
168
169
Called when `socket` is detached from a request and could be persisted by the
170
Agent. Default behavior is to:
171
172
```js
Aug 18, 2017
Aug 18, 2017
173
socket.setKeepAlive(true, this.keepAliveMsecs);
Jun 7, 2017
Jun 7, 2017
174
socket.unref();
Aug 18, 2017
Aug 18, 2017
175
return true;
Jun 7, 2017
Jun 7, 2017
176
```
177
178
This method can be overridden by a particular `Agent` subclass. If this
179
method returns a falsy value, the socket will be destroyed instead of persisting
180
it for use with the next request.
181
182
### agent.reuseSocket(socket, request)
183
<!-- YAML
Nov 16, 2017
Nov 16, 2017
184
added: v8.1.0
Jun 7, 2017
Jun 7, 2017
185
-->
186
187
* `socket` {net.Socket}
188
* `request` {http.ClientRequest}
189
190
Called when `socket` is attached to `request` after being persisted because of
191
the keep-alive options. Default behavior is to:
192
193
```js
194
socket.ref();
195
```
196
197
This method can be overridden by a particular `Agent` subclass.
198
Nov 13, 2015
Nov 13, 2015
199
### agent.destroy()
Jun 29, 2016
Jun 29, 2016
200
<!-- YAML
201
added: v0.11.4
202
-->
Jun 13, 2012
Jun 13, 2012
203
Nov 13, 2015
Nov 13, 2015
204
Destroy any sockets that are currently in use by the agent.
Jun 13, 2012
Jun 13, 2012
205
Mar 8, 2017
Mar 8, 2017
206
It is usually not necessary to do this. However, if using an
Jan 25, 2017
Jan 25, 2017
207
agent with `keepAlive` enabled, then it is best to explicitly shut down
Mar 8, 2017
Mar 8, 2017
208
the agent when it will no longer be used. Otherwise,
Nov 13, 2015
Nov 13, 2015
209
sockets may hang open for quite a long time before the server
210
terminates them.
Jun 13, 2012
Jun 13, 2012
211
Nov 13, 2015
Nov 13, 2015
212
### agent.freeSockets
Jun 29, 2016
Jun 29, 2016
213
<!-- YAML
214
added: v0.11.4
215
-->
Oct 28, 2010
Oct 28, 2010
216
Oct 11, 2016
Oct 11, 2016
217
* {Object}
218
Nov 13, 2015
Nov 13, 2015
219
An object which contains arrays of sockets currently awaiting use by
Jan 25, 2017
Jan 25, 2017
220
the agent when `keepAlive` is enabled. Do not modify.
Oct 28, 2010
Oct 28, 2010
221
Nov 13, 2015
Nov 13, 2015
222
### agent.getName(options)
Jun 29, 2016
Jun 29, 2016
223
<!-- YAML
224
added: v0.11.4
225
-->
Oct 28, 2010
Oct 28, 2010
226
Oct 11, 2016
Oct 11, 2016
227
* `options` {Object} A set of options providing information for name generation
Feb 23, 2018
Feb 23, 2018
228
* `host` {string} A domain name or IP address of the server to issue the
229
request to
Mar 2, 2017
Mar 2, 2017
230
* `port` {number} Port of remote server
231
* `localAddress` {string} Local interface to bind for network connections
Oct 11, 2016
Oct 11, 2016
232
when issuing the request
Aug 18, 2017
Aug 18, 2017
233
* `family` {integer} Must be 4 or 6 if this doesn't equal `undefined`.
Mar 8, 2017
Mar 8, 2017
234
* Returns: {string}
Oct 11, 2016
Oct 11, 2016
235
Nov 13, 2015
Nov 13, 2015
236
Get a unique name for a set of request options, to determine whether a
Aug 18, 2017
Aug 18, 2017
237
connection can be reused. For an HTTP agent, this returns
238
`host:port:localAddress` or `host:port:localAddress:family`. For an HTTPS agent,
239
the name includes the CA, cert, ciphers, and other HTTPS/TLS-specific options
240
that determine socket reusability.
Jan 15, 2012
Jan 15, 2012
241
Nov 13, 2015
Nov 13, 2015
242
### agent.maxFreeSockets
Jun 29, 2016
Jun 29, 2016
243
<!-- YAML
244
added: v0.11.7
245
-->
Jan 15, 2012
Jan 15, 2012
246
Mar 8, 2017
Mar 8, 2017
247
* {number}
Oct 11, 2016
Oct 11, 2016
248
Jan 25, 2017
Jan 25, 2017
249
By default set to 256. For agents with `keepAlive` enabled, this
Nov 13, 2015
Nov 13, 2015
250
sets the maximum number of sockets that will be left open in the free
251
state.
Jan 15, 2012
Jan 15, 2012
252
Nov 13, 2015
Nov 13, 2015
253
### agent.maxSockets
Jun 29, 2016
Jun 29, 2016
254
<!-- YAML
255
added: v0.3.6
256
-->
Mar 6, 2013
Mar 6, 2013
257
Mar 8, 2017
Mar 8, 2017
258
* {number}
Oct 11, 2016
Oct 11, 2016
259
Nov 13, 2015
Nov 13, 2015
260
By default set to Infinity. Determines how many concurrent sockets the agent
Aug 18, 2017
Aug 18, 2017
261
can have open per origin. Origin is the returned value of [`agent.getName()`][].
Mar 6, 2013
Mar 6, 2013
262
Nov 13, 2015
Nov 13, 2015
263
### agent.requests
Jun 29, 2016
Jun 29, 2016
264
<!-- YAML
265
added: v0.5.9
266
-->
Mar 6, 2013
Mar 6, 2013
267
Oct 11, 2016
Oct 11, 2016
268
* {Object}
269
Nov 13, 2015
Nov 13, 2015
270
An object which contains queues of requests that have not yet been assigned to
271
sockets. Do not modify.
Mar 6, 2013
Mar 6, 2013
272
Nov 13, 2015
Nov 13, 2015
273
### agent.sockets
Jun 29, 2016
Jun 29, 2016
274
<!-- YAML
275
added: v0.3.6
276
-->
May 16, 2015
May 16, 2015
277
Oct 11, 2016
Oct 11, 2016
278
* {Object}
279
Nov 13, 2015
Nov 13, 2015
280
An object which contains arrays of sockets currently in use by the
Jan 25, 2017
Jan 25, 2017
281
agent. Do not modify.
Mar 6, 2013
Mar 6, 2013
282
Nov 13, 2015
Nov 13, 2015
283
## Class: http.ClientRequest
Jun 29, 2016
Jun 29, 2016
284
<!-- YAML
285
added: v0.1.17
286
-->
Mar 6, 2013
Mar 6, 2013
287
Dec 3, 2015
Dec 3, 2015
288
This object is created internally and returned from [`http.request()`][]. It
Nov 13, 2015
Nov 13, 2015
289
represents an _in-progress_ request whose header has already been queued. The
Sep 15, 2017
Sep 15, 2017
290
header is still mutable using the [`setHeader(name, value)`][],
291
[`getHeader(name)`][], [`removeHeader(name)`][] API. The actual header will
292
be sent along with the first data chunk or when calling [`request.end()`][].
Mar 6, 2013
Mar 6, 2013
293
Jul 14, 2016
Jul 14, 2016
294
To get the response, add a listener for [`'response'`][] to the request object.
295
[`'response'`][] will be emitted from the request object when the response
296
headers have been received. The [`'response'`][] event is executed with one
Dec 3, 2015
Dec 3, 2015
297
argument which is an instance of [`http.IncomingMessage`][].
Mar 6, 2013
Mar 6, 2013
298
Jul 14, 2016
Jul 14, 2016
299
During the [`'response'`][] event, one can add listeners to the
Nov 13, 2015
Nov 13, 2015
300
response object; particularly to listen for the `'data'` event.
Oct 28, 2010
Oct 28, 2010
301
Jul 14, 2016
Jul 14, 2016
302
If no [`'response'`][] handler is added, then the response will be
Mar 8, 2017
Mar 8, 2017
303
entirely discarded. However, if a [`'response'`][] event handler is added,
304
then the data from the response object **must** be consumed, either by
Nov 13, 2015
Nov 13, 2015
305
calling `response.read()` whenever there is a `'readable'` event, or
306
by adding a `'data'` handler, or by calling the `.resume()` method.
307
Until the data is consumed, the `'end'` event will not fire. Also, until
308
the data is read it will consume memory that can eventually lead to a
309
'process out of memory' error.
Oct 28, 2010
Oct 28, 2010
310
Feb 8, 2018
Feb 8, 2018
311
Node.js does not check whether Content-Length and the length of the
May 25, 2017
May 25, 2017
312
body which has been transmitted are equal or not.
Feb 5, 2012
Feb 5, 2012
313
Nov 13, 2015
Nov 13, 2015
314
The request implements the [Writable Stream][] interface. This is an
Dec 3, 2015
Dec 3, 2015
315
[`EventEmitter`][] with the following events:
Oct 28, 2010
Oct 28, 2010
316
Nov 13, 2015
Nov 13, 2015
317
### Event: 'abort'
Jun 29, 2016
Jun 29, 2016
318
<!-- YAML
319
added: v1.4.1
320
-->
Nov 28, 2013
Nov 28, 2013
321
Nov 13, 2015
Nov 13, 2015
322
Emitted when the request has been aborted by the client. This event is only
323
emitted on the first call to `abort()`.
Oct 28, 2010
Oct 28, 2010
324
Nov 13, 2015
Nov 13, 2015
325
### Event: 'connect'
Jun 29, 2016
Jun 29, 2016
326
<!-- YAML
327
added: v0.7.0
328
-->
Oct 28, 2010
Oct 28, 2010
329
Oct 11, 2016
Oct 11, 2016
330
* `response` {http.IncomingMessage}
331
* `socket` {net.Socket}
332
* `head` {Buffer}
Oct 28, 2010
Oct 28, 2010
333
Feb 23, 2018
Feb 23, 2018
334
Emitted each time a server responds to a request with a `CONNECT` method. If
335
this event is not being listened for, clients receiving a `CONNECT` method will
336
have their connections closed.
Oct 28, 2010
Oct 28, 2010
337
Mar 8, 2017
Mar 8, 2017
338
A client and server pair demonstrating how to listen for the `'connect'` event:
Oct 28, 2010
Oct 28, 2010
339
Jan 21, 2016
Jan 21, 2016
340
```js
341
const http = require('http');
342
const net = require('net');
343
const url = require('url');
344
345
// Create an HTTP tunneling proxy
Apr 24, 2017
Apr 24, 2017
346
const proxy = http.createServer((req, res) => {
Jun 2, 2017
Jun 2, 2017
347
res.writeHead(200, { 'Content-Type': 'text/plain' });
Jan 21, 2016
Jan 21, 2016
348
res.end('okay');
349
});
350
proxy.on('connect', (req, cltSocket, head) => {
351
// connect to an origin server
Apr 4, 2017
Apr 4, 2017
352
const srvUrl = url.parse(`http://${req.url}`);
353
const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
Jan 21, 2016
Jan 21, 2016
354
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
355
'Proxy-agent: Node.js-Proxy\r\n' +
356
'\r\n');
357
srvSocket.write(head);
358
srvSocket.pipe(cltSocket);
359
cltSocket.pipe(srvSocket);
360
});
361
});
362
363
// now that proxy is running
364
proxy.listen(1337, '127.0.0.1', () => {
365
366
// make a request to a tunneling proxy
Apr 4, 2017
Apr 4, 2017
367
const options = {
Jan 21, 2016
Jan 21, 2016
368
port: 1337,
369
hostname: '127.0.0.1',
370
method: 'CONNECT',
371
path: 'www.google.com:80'
372
};
373
Apr 4, 2017
Apr 4, 2017
374
const req = http.request(options);
Jan 21, 2016
Jan 21, 2016
375
req.end();
376
377
req.on('connect', (res, socket, head) => {
378
console.log('got connected!');
379
380
// make a request over an HTTP tunnel
381
socket.write('GET / HTTP/1.1\r\n' +
382
'Host: www.google.com:80\r\n' +
383
'Connection: close\r\n' +
384
'\r\n');
385
socket.on('data', (chunk) => {
386
console.log(chunk.toString());
Nov 13, 2015
Nov 13, 2015
387
});
Jan 21, 2016
Jan 21, 2016
388
socket.on('end', () => {
389
proxy.close();
Nov 13, 2015
Nov 13, 2015
390
});
Jan 21, 2016
Jan 21, 2016
391
});
392
});
393
```
Mar 6, 2013
Mar 6, 2013
394
Nov 13, 2015
Nov 13, 2015
395
### Event: 'continue'
Jun 29, 2016
Jun 29, 2016
396
<!-- YAML
397
added: v0.3.2
398
-->
Mar 6, 2013
Mar 6, 2013
399
Nov 13, 2015
Nov 13, 2015
400
Emitted when the server sends a '100 Continue' HTTP response, usually because
401
the request contained 'Expect: 100-continue'. This is an instruction that
402
the client should send the request body.
Feb 10, 2011
Feb 10, 2011
403
Feb 15, 2018
Feb 15, 2018
404
### Event: 'information'
405
<!-- YAML
406
added: REPLACEME
407
-->
408
409
Emitted when the server sends a 1xx response (excluding 101 Upgrade). This
410
event is emitted with a callback containing an object with a status code.
411
412
```js
413
const http = require('http');
414
415
const options = {
416
hostname: '127.0.0.1',
417
port: 8080,
418
path: '/length_request'
419
};
420
421
// Make a request
422
const req = http.request(options);
423
req.end();
424
425
req.on('information', (res) => {
Feb 22, 2018
Feb 22, 2018
426
console.log(`Got information prior to main response: ${res.statusCode}`);
Feb 15, 2018
Feb 15, 2018
427
});
428
```
429
430
101 Upgrade statuses do not fire this event due to their break from the
431
traditional HTTP request/response chain, such as web sockets, in-place TLS
432
upgrades, or HTTP 2.0. To be notified of 101 Upgrade notices, listen for the
433
[`'upgrade'`][] event instead.
434
Nov 13, 2015
Nov 13, 2015
435
### Event: 'response'
Jun 29, 2016
Jun 29, 2016
436
<!-- YAML
437
added: v0.1.0
438
-->
Feb 10, 2011
Feb 10, 2011
439
Oct 11, 2016
Oct 11, 2016
440
* `response` {http.IncomingMessage}
Feb 10, 2011
Feb 10, 2011
441
Nov 13, 2015
Nov 13, 2015
442
Emitted when a response is received to this request. This event is emitted only
Oct 11, 2016
Oct 11, 2016
443
once.
Feb 10, 2011
Feb 10, 2011
444
Nov 13, 2015
Nov 13, 2015
445
### Event: 'socket'
Jun 29, 2016
Jun 29, 2016
446
<!-- YAML
447
added: v0.5.3
448
-->
Oct 17, 2013
Oct 17, 2013
449
Oct 11, 2016
Oct 11, 2016
450
* `socket` {net.Socket}
Oct 17, 2013
Oct 17, 2013
451
Nov 13, 2015
Nov 13, 2015
452
Emitted after a socket is assigned to this request.
Oct 17, 2013
Oct 17, 2013
453
Sep 20, 2017
Sep 20, 2017
454
### Event: 'timeout'
455
<!-- YAML
456
added: v0.7.8
457
-->
458
459
Emitted when the underlying socket times out from inactivity. This only notifies
460
that the socket has been idle. The request must be aborted manually.
461
462
See also: [`request.setTimeout()`][]
463
Nov 13, 2015
Nov 13, 2015
464
### Event: 'upgrade'
Jun 29, 2016
Jun 29, 2016
465
<!-- YAML
466
added: v0.1.94
467
-->
Oct 17, 2013
Oct 17, 2013
468
Oct 11, 2016
Oct 11, 2016
469
* `response` {http.IncomingMessage}
470
* `socket` {net.Socket}
471
* `head` {Buffer}
Feb 10, 2011
Feb 10, 2011
472
Nov 13, 2015
Nov 13, 2015
473
Emitted each time a server responds to a request with an upgrade. If this
Jan 27, 2017
Jan 27, 2017
474
event is not being listened for, clients receiving an upgrade header will have
Nov 13, 2015
Nov 13, 2015
475
their connections closed.
Feb 10, 2011
Feb 10, 2011
476
Mar 8, 2017
Mar 8, 2017
477
A client server pair demonstrating how to listen for the `'upgrade'` event.
Feb 10, 2011
Feb 10, 2011
478
Jan 21, 2016
Jan 21, 2016
479
```js
480
const http = require('http');
Feb 10, 2011
Feb 10, 2011
481
Jan 21, 2016
Jan 21, 2016
482
// Create an HTTP server
Apr 24, 2017
Apr 24, 2017
483
const srv = http.createServer((req, res) => {
Jun 2, 2017
Jun 2, 2017
484
res.writeHead(200, { 'Content-Type': 'text/plain' });
Jan 21, 2016
Jan 21, 2016
485
res.end('okay');
486
});
487
srv.on('upgrade', (req, socket, head) => {
488
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
489
'Upgrade: WebSocket\r\n' +
490
'Connection: Upgrade\r\n' +
491
'\r\n');
492
493
socket.pipe(socket); // echo back
494
});
495
496
// now that server is running
497
srv.listen(1337, '127.0.0.1', () => {
498
499
// make a request
Apr 4, 2017
Apr 4, 2017
500
const options = {
Jan 21, 2016
Jan 21, 2016
501
port: 1337,
502
hostname: '127.0.0.1',
503
headers: {
504
'Connection': 'Upgrade',
505
'Upgrade': 'websocket'
506
}
507
};
508
Apr 4, 2017
Apr 4, 2017
509
const req = http.request(options);
Jan 21, 2016
Jan 21, 2016
510
req.end();
511
512
req.on('upgrade', (res, socket, upgradeHead) => {
513
console.log('got upgraded!');
514
socket.end();
515
process.exit(0);
516
});
517
});
518
```
Feb 15, 2012
Feb 15, 2012
519
Nov 13, 2015
Nov 13, 2015
520
### request.abort()
Jun 29, 2016
Jun 29, 2016
521
<!-- YAML
522
added: v0.3.8
523
-->
Feb 15, 2012
Feb 15, 2012
524
Nov 13, 2015
Nov 13, 2015
525
Marks the request as aborting. Calling this will cause remaining data
526
in the response to be dropped and the socket to be destroyed.
Feb 10, 2011
Feb 10, 2011
527
Feb 27, 2017
Feb 27, 2017
528
### request.aborted
529
<!-- YAML
530
added: v0.11.14
531
-->
532
533
If a request has been aborted, this value is the time when the request was
534
aborted, in milliseconds since 1 January 1970 00:00:00 UTC.
535
Jun 19, 2017
Jun 19, 2017
536
### request.connection
537
<!-- YAML
538
added: v0.3.0
539
-->
540
541
* {net.Socket}
542
543
See [`request.socket`][]
544
Jun 13, 2017
Jun 13, 2017
545
### request.end([data[, encoding]][, callback])
Jun 29, 2016
Jun 29, 2016
546
<!-- YAML
547
added: v0.1.90
Feb 19, 2018
Feb 19, 2018
548
changes:
549
- version: REPLACEME
550
pr-url: https://github.com/nodejs/node/pull/18780
551
description: This method now returns a reference to `ClientRequest`.
Jun 29, 2016
Jun 29, 2016
552
-->
Feb 10, 2011
Feb 10, 2011
553
Mar 8, 2017
Mar 8, 2017
554
* `data` {string|Buffer}
Mar 2, 2017
Mar 2, 2017
555
* `encoding` {string}
Oct 11, 2016
Oct 11, 2016
556
* `callback` {Function}
Feb 19, 2018
Feb 19, 2018
557
* Returns: {this}
Oct 11, 2016
Oct 11, 2016
558
Nov 13, 2015
Nov 13, 2015
559
Finishes sending the request. If any parts of the body are
560
unsent, it will flush them to the stream. If the request is
561
chunked, this will send the terminating `'0\r\n\r\n'`.
Feb 10, 2011
Feb 10, 2011
562
Nov 13, 2015
Nov 13, 2015
563
If `data` is specified, it is equivalent to calling
Jul 9, 2017
Jul 9, 2017
564
[`request.write(data, encoding)`][] followed by `request.end(callback)`.
Feb 10, 2011
Feb 10, 2011
565
Nov 13, 2015
Nov 13, 2015
566
If `callback` is specified, it will be called when the request stream
567
is finished.
Feb 10, 2011
Feb 10, 2011
568
Nov 13, 2015
Nov 13, 2015
569
### request.flushHeaders()
Jun 29, 2016
Jun 29, 2016
570
<!-- YAML
571
added: v1.6.0
572
-->
Feb 10, 2011
Feb 10, 2011
573
Nov 13, 2015
Nov 13, 2015
574
Flush the request headers.
Feb 10, 2011
Feb 10, 2011
575
Mar 8, 2017
Mar 8, 2017
576
For efficiency reasons, Node.js normally buffers the request headers until
577
`request.end()` is called or the first chunk of request data is written. It
578
then tries to pack the request headers and data into a single TCP packet.
Feb 10, 2011
Feb 10, 2011
579
Mar 8, 2017
Mar 8, 2017
580
That's usually desired (it saves a TCP round-trip), but not when the first
581
data is not sent until possibly much later. `request.flushHeaders()` bypasses
582
the optimization and kickstarts the request.
Feb 10, 2011
Feb 10, 2011
583
Sep 15, 2017
Sep 15, 2017
584
### request.getHeader(name)
585
<!-- YAML
586
added: v1.6.0
587
-->
588
589
* `name` {string}
590
* Returns: {string}
591
592
Reads out a header on the request. Note that the name is case insensitive.
593
594
Example:
595
```js
596
const contentType = request.getHeader('Content-Type');
597
```
598
599
### request.removeHeader(name)
600
<!-- YAML
601
added: v1.6.0
602
-->
603
604
* `name` {string}
605
606
Removes a header that's already defined into headers object.
607
608
Example:
609
```js
610
request.removeHeader('Content-Type');
611
```
612
613
### request.setHeader(name, value)
614
<!-- YAML
615
added: v1.6.0
616
-->
617
618
* `name` {string}
619
* `value` {string}
620
621
Sets a single header value for headers object. If this header already exists in
622
the to-be-sent headers, its value will be replaced. Use an array of strings
623
here to send multiple headers with the same name.
624
625
Example:
626
```js
627
request.setHeader('Content-Type', 'application/json');
628
```
629
630
or
631
632
```js
633
request.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
634
```
635
Nov 13, 2015
Nov 13, 2015
636
### request.setNoDelay([noDelay])
Jun 29, 2016
Jun 29, 2016
637
<!-- YAML
638
added: v0.5.9
639
-->
Feb 10, 2011
Feb 10, 2011
640
Mar 2, 2017
Mar 2, 2017
641
* `noDelay` {boolean}
Oct 11, 2016
Oct 11, 2016
642
Nov 13, 2015
Nov 13, 2015
643
Once a socket is assigned to this request and is connected
Dec 3, 2015
Dec 3, 2015
644
[`socket.setNoDelay()`][] will be called.
Oct 28, 2010
Oct 28, 2010
645
Nov 13, 2015
Nov 13, 2015
646
### request.setSocketKeepAlive([enable][, initialDelay])
Jun 29, 2016
Jun 29, 2016
647
<!-- YAML
648
added: v0.5.9
649
-->
Feb 10, 2011
Feb 10, 2011
650
Mar 2, 2017
Mar 2, 2017
651
* `enable` {boolean}
652
* `initialDelay` {number}
Oct 11, 2016
Oct 11, 2016
653
Nov 13, 2015
Nov 13, 2015
654
Once a socket is assigned to this request and is connected
Dec 3, 2015
Dec 3, 2015
655
[`socket.setKeepAlive()`][] will be called.
Oct 28, 2010
Oct 28, 2010
656
Nov 13, 2015
Nov 13, 2015
657
### request.setTimeout(timeout[, callback])
Jun 29, 2016
Jun 29, 2016
658
<!-- YAML
659
added: v0.5.9
660
-->
Oct 28, 2010
Oct 28, 2010
661
Jan 18, 2018
Jan 18, 2018
662
* `timeout` {number} Milliseconds before a request times out.
Feb 23, 2018
Feb 23, 2018
663
* `callback` {Function} Optional function to be called when a timeout occurs.
664
Same as binding to the `timeout` event.
Dec 30, 2015
Dec 30, 2015
665
Oct 11, 2016
Oct 11, 2016
666
Once a socket is assigned to this request and is connected
667
[`socket.setTimeout()`][] will be called.
668
Sep 3, 2016
Sep 3, 2016
669
Returns `request`.
670
Jun 19, 2017
Jun 19, 2017
671
### request.socket
672
<!-- YAML
673
added: v0.3.0
674
-->
675
676
* {net.Socket}
677
678
Reference to the underlying socket. Usually users will not want to access
679
this property. In particular, the socket will not emit `'readable'` events
680
because of how the protocol parser attaches to the socket. After
681
`response.end()`, the property is nulled. The `socket` may also be accessed
682
via `request.connection`.
683
684
Example:
685
686
```js
687
const http = require('http');
Aug 18, 2017
Aug 18, 2017
688
const options = {
689
host: 'www.google.com',
690
};
691
const req = http.get(options);
692
req.end();
693
req.once('response', (res) => {
694
const ip = req.socket.localAddress;
695
const port = req.socket.localPort;
696
console.log(`Your IP address is ${ip} and your source port is ${port}.`);
697
// consume response object
698
});
Jun 19, 2017
Jun 19, 2017
699
```
700
Nov 13, 2015
Nov 13, 2015
701
### request.write(chunk[, encoding][, callback])
Jun 29, 2016
Jun 29, 2016
702
<!-- YAML
703
added: v0.1.29
704
-->
Oct 28, 2010
Oct 28, 2010
705
Mar 8, 2017
Mar 8, 2017
706
* `chunk` {string|Buffer}
Mar 2, 2017
Mar 2, 2017
707
* `encoding` {string}
Oct 11, 2016
Oct 11, 2016
708
* `callback` {Function}
709
Nov 13, 2015
Nov 13, 2015
710
Sends a chunk of the body. By calling this method
Mar 8, 2017
Mar 8, 2017
711
many times, a request body can be sent to a
Nov 13, 2015
Nov 13, 2015
712
server--in that case it is suggested to use the
713
`['Transfer-Encoding', 'chunked']` header line when
714
creating the request.
Jul 31, 2012
Jul 31, 2012
715
Nov 13, 2015
Nov 13, 2015
716
The `encoding` argument is optional and only applies when `chunk` is a string.
717
Defaults to `'utf8'`.
Oct 28, 2010
Oct 28, 2010
718
Nov 13, 2015
Nov 13, 2015
719
The `callback` argument is optional and will be called when this chunk of data
720
is flushed.
Oct 28, 2010
Oct 28, 2010
721
Feb 6, 2018
Feb 6, 2018
722
Returns `true` if the entire data was flushed successfully to the kernel
723
buffer. Returns `false` if all or part of the data was queued in user memory.
724
`'drain'` will be emitted when the buffer is free again.
Oct 28, 2010
Oct 28, 2010
725
Nov 13, 2015
Nov 13, 2015
726
## Class: http.Server
Jun 29, 2016
Jun 29, 2016
727
<!-- YAML
728
added: v0.1.17
729
-->
Oct 28, 2010
Oct 28, 2010
730
Feb 23, 2018
Feb 23, 2018
731
This class inherits from [`net.Server`][] and has the following additional
732
events:
Oct 28, 2010
Oct 28, 2010
733
Nov 13, 2015
Nov 13, 2015
734
### Event: 'checkContinue'
Jun 29, 2016
Jun 29, 2016
735
<!-- YAML
736
added: v0.3.0
737
-->
Oct 28, 2010
Oct 28, 2010
738
Oct 11, 2016
Oct 11, 2016
739
* `request` {http.IncomingMessage}
740
* `response` {http.ServerResponse}
Oct 28, 2010
Oct 28, 2010
741
Oct 11, 2016
Oct 11, 2016
742
Emitted each time a request with an HTTP `Expect: 100-continue` is received.
Jan 27, 2017
Jan 27, 2017
743
If this event is not listened for, the server will automatically respond
Oct 11, 2016
Oct 11, 2016
744
with a `100 Continue` as appropriate.
Oct 28, 2010
Oct 28, 2010
745
Feb 23, 2018
Feb 23, 2018
746
Handling this event involves calling [`response.writeContinue()`][] if the
747
client should continue to send the request body, or generating an appropriate
748
HTTP response (e.g. 400 Bad Request) if the client should not continue to send
749
the request body.
Oct 28, 2010
Oct 28, 2010
750
Oct 11, 2016
Oct 11, 2016
751
Note that when this event is emitted and handled, the [`'request'`][] event will
752
not be emitted.
753
754
### Event: 'checkExpectation'
755
<!-- YAML
756
added: v5.5.0
757
-->
758
Aug 18, 2017
Aug 18, 2017
759
* `request` {http.IncomingMessage}
Oct 11, 2016
Oct 11, 2016
760
* `response` {http.ServerResponse}
761
762
Emitted each time a request with an HTTP `Expect` header is received, where the
Jan 27, 2017
Jan 27, 2017
763
value is not `100-continue`. If this event is not listened for, the server will
Oct 11, 2016
Oct 11, 2016
764
automatically respond with a `417 Expectation Failed` as appropriate.
765
766
Note that when this event is emitted and handled, the [`'request'`][] event will
Nov 13, 2015
Nov 13, 2015
767
not be emitted.
Aug 23, 2015
Aug 23, 2015
768
Nov 13, 2015
Nov 13, 2015
769
### Event: 'clientError'
Jun 29, 2016
Jun 29, 2016
770
<!-- YAML
771
added: v0.1.94
Feb 24, 2017
Feb 24, 2017
772
changes:
773
- version: v6.0.0
774
pr-url: https://github.com/nodejs/node/pull/4557
775
description: The default action of calling `.destroy()` on the `socket`
776
will no longer take place if there are listeners attached
777
for `clientError`.
Jan 10, 2018
Jan 10, 2018
778
- version: v9.4.0
Dec 27, 2017
Dec 27, 2017
779
pr-url: https://github.com/nodejs/node/pull/17672
780
description: The rawPacket is the current buffer that just parsed. Adding
781
this buffer to the error object of clientError event is to make
782
it possible that developers can log the broken packet.
Jun 29, 2016
Jun 29, 2016
783
-->
Aug 23, 2015
Aug 23, 2015
784
Oct 11, 2016
Oct 11, 2016
785
* `exception` {Error}
786
* `socket` {net.Socket}
Oct 28, 2010
Oct 28, 2010
787
Dec 3, 2015
Dec 3, 2015
788
If a client connection emits an `'error'` event, it will be forwarded here.
Jan 7, 2016
Jan 7, 2016
789
Listener of this event is responsible for closing/destroying the underlying
Feb 23, 2018
Feb 23, 2018
790
socket. For example, one may wish to more gracefully close the socket with a
791
custom HTTP response instead of abruptly severing the connection.
Jan 7, 2016
Jan 7, 2016
792
Feb 23, 2018
Feb 23, 2018
793
Default behavior is to close the socket with an HTTP '400 Bad Request' response
794
if possible, otherwise the socket is immediately destroyed.
May 16, 2012
May 16, 2012
795
Dec 3, 2015
Dec 3, 2015
796
`socket` is the [`net.Socket`][] object that the error originated from.
Oct 28, 2010
Oct 28, 2010
797
Feb 17, 2016
Feb 17, 2016
798
```js
799
const http = require('http');
800
801
const server = http.createServer((req, res) => {
802
res.end();
803
});
804
server.on('clientError', (err, socket) => {
805
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
806
});
807
server.listen(8000);
808
```
809
810
When the `'clientError'` event occurs, there is no `request` or `response`
811
object, so any HTTP response sent, including response headers and payload,
812
*must* be written directly to the `socket` object. Care must be taken to
813
ensure the response is a properly formatted HTTP response message.
814
Dec 27, 2017
Dec 27, 2017
815
`err` is an instance of `Error` with two extra columns:
816
817
+ `bytesParsed`: the bytes count of request packet that Node.js may have parsed
818
correctly;
819
+ `rawPacket`: the raw packet of current request.
820
Nov 13, 2015
Nov 13, 2015
821
### Event: 'close'
Jun 29, 2016
Jun 29, 2016
822
<!-- YAML
823
added: v0.1.4
824
-->
Oct 28, 2010
Oct 28, 2010
825
Nov 13, 2015
Nov 13, 2015
826
Emitted when the server closes.
Nov 28, 2013
Nov 28, 2013
827
Nov 13, 2015
Nov 13, 2015
828
### Event: 'connect'
Jun 29, 2016
Jun 29, 2016
829
<!-- YAML
830
added: v0.7.0
831
-->
Jan 21, 2011
Jan 21, 2011
832
Oct 11, 2016
Oct 11, 2016
833
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
834
the [`'request'`][] event
835
* `socket` {net.Socket} Network socket between the server and client
836
* `head` {Buffer} The first packet of the tunneling stream (may be empty)
Jan 21, 2011
Jan 21, 2011
837
Jan 27, 2017
Jan 27, 2017
838
Emitted each time a client requests an HTTP `CONNECT` method. If this event is
839
not listened for, then clients requesting a `CONNECT` method will have their
Nov 13, 2015
Nov 13, 2015
840
connections closed.
Sep 17, 2014
Sep 17, 2014
841
Dec 3, 2015
Dec 3, 2015
842
After this event is emitted, the request's socket will not have a `'data'`
Mar 8, 2017
Mar 8, 2017
843
event listener, meaning it will need to be bound in order to handle data
Nov 13, 2015
Nov 13, 2015
844
sent to the server on that socket.
Oct 28, 2010
Oct 28, 2010
845
Nov 13, 2015
Nov 13, 2015
846
### Event: 'connection'
Jun 29, 2016
Jun 29, 2016
847
<!-- YAML
848
added: v0.1.0
849
-->
May 5, 2011
May 5, 2011
850
Oct 11, 2016
Oct 11, 2016
851
* `socket` {net.Socket}
Jan 21, 2011
Jan 21, 2011
852
Oct 23, 2017
Oct 23, 2017
853
This event is emitted when a new TCP stream is established. `socket` is
854
typically an object of type [`net.Socket`][]. Usually users will not want to
855
access this event. In particular, the socket will not emit `'readable'` events
856
because of how the protocol parser attaches to the socket. The `socket` can
857
also be accessed at `request.connection`.
858
Feb 8, 2018
Feb 8, 2018
859
This event can also be explicitly emitted by users to inject connections
Oct 23, 2017
Oct 23, 2017
860
into the HTTP server. In that case, any [`Duplex`][] stream can be passed.
Oct 28, 2010
Oct 28, 2010
861
Nov 13, 2015
Nov 13, 2015
862
### Event: 'request'
Jun 29, 2016
Jun 29, 2016
863
<!-- YAML
864
added: v0.1.0
865
-->
Jan 21, 2011
Jan 21, 2011
866
Oct 11, 2016
Oct 11, 2016
867
* `request` {http.IncomingMessage}
868
* `response` {http.ServerResponse}
Oct 28, 2010
Oct 28, 2010
869
Nov 13, 2015
Nov 13, 2015
870
Emitted each time there is a request. Note that there may be multiple requests
Jan 25, 2017
Jan 25, 2017
871
per connection (in the case of HTTP Keep-Alive connections).
Oct 28, 2010
Oct 28, 2010
872
Nov 13, 2015
Nov 13, 2015
873
### Event: 'upgrade'
Jun 29, 2016
Jun 29, 2016
874
<!-- YAML
875
added: v0.1.94
876
-->
Oct 28, 2010
Oct 28, 2010
877
Oct 11, 2016
Oct 11, 2016
878
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
879
the [`'request'`][] event
880
* `socket` {net.Socket} Network socket between the server and client
881
* `head` {Buffer} The first packet of the upgraded stream (may be empty)
Oct 28, 2010
Oct 28, 2010
882
Jan 27, 2017
Jan 27, 2017
883
Emitted each time a client requests an HTTP upgrade. If this event is not
Nov 13, 2015
Nov 13, 2015
884
listened for, then clients requesting an upgrade will have their connections
885
closed.
Oct 22, 2011
Oct 22, 2011
886
Dec 3, 2015
Dec 3, 2015
887
After this event is emitted, the request's socket will not have a `'data'`
Mar 8, 2017
Mar 8, 2017
888
event listener, meaning it will need to be bound in order to handle data
Nov 13, 2015
Nov 13, 2015
889
sent to the server on that socket.
Jan 21, 2011
Jan 21, 2011
890
Nov 13, 2015
Nov 13, 2015
891
### server.close([callback])
Jun 29, 2016
Jun 29, 2016
892
<!-- YAML
893
added: v0.1.90
894
-->
Jan 21, 2011
Jan 21, 2011
895
Oct 11, 2016
Oct 11, 2016
896
* `callback` {Function}
897
Dec 3, 2015
Dec 3, 2015
898
Stops the server from accepting new connections. See [`net.Server.close()`][].
Jan 21, 2011
Jan 21, 2011
899
Oct 19, 2017
Oct 19, 2017
900
### server.listen()
Aug 29, 2016
Aug 29, 2016
901
Oct 19, 2017
Oct 19, 2017
902
Starts the HTTP server listening for connections.
903
This method is identical to [`server.listen()`][] from [`net.Server`][].
Aug 15, 2013
Aug 15, 2013
904
Jan 21, 2016
Jan 21, 2016
905
### server.listening
Jun 29, 2016
Jun 29, 2016
906
<!-- YAML
907
added: v5.7.0
908
-->
Jan 21, 2016
Jan 21, 2016
909
Mar 8, 2017
Mar 8, 2017
910
* {boolean}
Oct 11, 2016
Oct 11, 2016
911
Jan 21, 2016
Jan 21, 2016
912
A Boolean indicating whether or not the server is listening for
913
connections.
914
Mar 27, 2017
Mar 27, 2017
915
### server.maxHeadersCount
Jun 29, 2016
Jun 29, 2016
916
<!-- YAML
917
added: v0.7.0
918
-->
Jul 26, 2011
Jul 26, 2011
919
Mar 27, 2017
Mar 27, 2017
920
* {number} Defaults to 2000.
Oct 11, 2016
Oct 11, 2016
921
Mar 27, 2017
Mar 27, 2017
922
Limits maximum incoming headers count, equal to 2000 by default. If set to 0 -
Nov 13, 2015
Nov 13, 2015
923
no limit will be applied.
Aug 15, 2013
Aug 15, 2013
924
Mar 8, 2017
Mar 8, 2017
925
### server.setTimeout([msecs][, callback])
Jun 29, 2016
Jun 29, 2016
926
<!-- YAML
927
added: v0.9.12
928
-->
Aug 15, 2013
Aug 15, 2013
929
Mar 8, 2017
Mar 8, 2017
930
* `msecs` {number} Defaults to 120000 (2 minutes).
Nov 13, 2015
Nov 13, 2015
931
* `callback` {Function}
Jul 26, 2011
Jul 26, 2011
932
Nov 13, 2015
Nov 13, 2015
933
Sets the timeout value for sockets, and emits a `'timeout'` event on
934
the Server object, passing the socket as an argument, if a timeout
935
occurs.
Jul 26, 2011
Jul 26, 2011
936
Nov 13, 2015
Nov 13, 2015
937
If there is a `'timeout'` event listener on the Server object, then it
938
will be called with the timed-out socket as an argument.
Aug 15, 2013
Aug 15, 2013
939
Nov 13, 2015
Nov 13, 2015
940
By default, the Server's timeout value is 2 minutes, and sockets are
Mar 8, 2017
Mar 8, 2017
941
destroyed automatically if they time out. However, if a callback is assigned
942
to the Server's `'timeout'` event, timeouts must be handled explicitly.
Aug 15, 2013
Aug 15, 2013
943
Nov 13, 2015
Nov 13, 2015
944
Returns `server`.
Jul 26, 2011
Jul 26, 2011
945
Mar 27, 2017
Mar 27, 2017
946
### server.timeout
Jun 29, 2016
Jun 29, 2016
947
<!-- YAML
948
added: v0.9.12
949
-->
Jul 26, 2011
Jul 26, 2011
950
May 26, 2017
May 26, 2017
951
* {number} Timeout in milliseconds. Defaults to 120000 (2 minutes).
Jul 26, 2011
Jul 26, 2011
952
Nov 13, 2015
Nov 13, 2015
953
The number of milliseconds of inactivity before a socket is presumed
954
to have timed out.
Aug 15, 2013
Aug 15, 2013
955
Dec 28, 2017
Dec 28, 2017
956
A value of `0` will disable the timeout behavior on incoming connections.
Aug 15, 2013
Aug 15, 2013
957
Feb 8, 2018
Feb 8, 2018
958
The socket timeout logic is set up on connection, so changing this
May 26, 2017
May 26, 2017
959
value only affects new connections to the server, not any existing connections.
960
961
### server.keepAliveTimeout
962
<!-- YAML
May 30, 2017
May 30, 2017
963
added: v8.0.0
May 26, 2017
May 26, 2017
964
-->
965
966
* {number} Timeout in milliseconds. Defaults to 5000 (5 seconds).
967
968
The number of milliseconds of inactivity a server needs to wait for additional
969
incoming data, after it has finished writing the last response, before a socket
970
will be destroyed. If the server receives new data before the keep-alive
971
timeout has fired, it will reset the regular inactivity timeout, i.e.,
972
[`server.timeout`][].
973
Feb 23, 2018
Feb 23, 2018
974
A value of `0` will disable the keep-alive timeout behavior on incoming
975
connections.
976
A value of `0` makes the http server behave similarly to Node.js versions prior
977
to 8.0.0, which did not have a keep-alive timeout.
May 26, 2017
May 26, 2017
978
Feb 8, 2018
Feb 8, 2018
979
The socket timeout logic is set up on connection, so changing this value only
980
affects new connections to the server, not any existing connections.
Aug 15, 2013
Aug 15, 2013
981
Nov 13, 2015
Nov 13, 2015
982
## Class: http.ServerResponse
Jun 29, 2016
Jun 29, 2016
983
<!-- YAML
984
added: v0.1.17
985
-->
Aug 15, 2013
Aug 15, 2013
986
Oct 11, 2016
Oct 11, 2016
987
This object is created internally by an HTTP server--not by the user. It is
988
passed as the second parameter to the [`'request'`][] event.
Aug 15, 2013
Aug 15, 2013
989
May 1, 2016
May 1, 2016
990
The response implements, but does not inherit from, the [Writable Stream][]
991
interface. This is an [`EventEmitter`][] with the following events:
Aug 15, 2013
Aug 15, 2013
992
Nov 13, 2015
Nov 13, 2015
993
### Event: 'close'
Jun 29, 2016
Jun 29, 2016
994
<!-- YAML
995
added: v0.6.7
996
-->
Feb 27, 2012
Feb 27, 2012
997
Nov 13, 2015
Nov 13, 2015
998
Indicates that the underlying connection was terminated before
Dec 3, 2015
Dec 3, 2015
999
[`response.end()`][] was called or able to flush.
Jul 26, 2011
Jul 26, 2011
1000