Skip to content

Latest commit

Β 

History

History
2100 lines (1634 loc) Β· 59.6 KB

File metadata and controls

2100 lines (1634 loc) Β· 59.6 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
Apr 4, 2018
Apr 4, 2018
12
careful to never buffer entire requests or responses β€” the
Oct 28, 2010
Oct 28, 2010
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`
Apr 4, 2018
Apr 4, 2018
36
property, which is an array of `[key, value, key2, value2, ...]`. For
Aug 15, 2013
Aug 15, 2013
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.
May 2, 2018
May 2, 2018
74
(see [`socket.unref()`]).
Jan 25, 2017
Jan 25, 2017
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
Apr 4, 2018
Apr 4, 2018
119
having to reestablish a TCP connection. **Default:** `false`.
Mar 8, 2017
Mar 8, 2017
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
Apr 4, 2018
Apr 4, 2018
123
`keepAlive` option is `false` or `undefined`. **Default:** `1000`.
Mar 2, 2017
Mar 2, 2017
124
* `maxSockets` {number} Maximum number of sockets to allow per
Apr 4, 2018
Apr 4, 2018
125
host. **Default:** `Infinity`.
Mar 2, 2017
Mar 2, 2017
126
* `maxFreeSockets` {number} Maximum number of sockets to leave open
Apr 4, 2018
Apr 4, 2018
127
in a free state. Only relevant if `keepAlive` is set to `true`.
Apr 4, 2018
Apr 4, 2018
128
**Default:** `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
May 2, 2018
May 2, 2018
170
`Agent`. Default behavior is to:
Jun 7, 2017
Jun 7, 2017
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
Apr 4, 2018
Apr 4, 2018
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
Apr 4, 2018
Apr 4, 2018
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
Apr 4, 2018
Apr 4, 2018
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
Apr 4, 2018
Apr 4, 2018
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
May 2, 2018
May 2, 2018
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
Apr 4, 2018
Apr 4, 2018
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
Apr 4, 2018
Apr 4, 2018
288
This object is created internally and returned from [`http.request()`][]. It
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)`][],
Apr 4, 2018
Apr 4, 2018
291
[`getHeader(name)`][], [`removeHeader(name)`][] API. The actual header will
Sep 15, 2017
Sep 15, 2017
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
Apr 4, 2018
Apr 4, 2018
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
Apr 4, 2018
Apr 4, 2018
303
entirely discarded. However, if a [`'response'`][] event handler is added,
Mar 8, 2017
Mar 8, 2017
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.
Apr 4, 2018
Apr 4, 2018
307
Until the data is consumed, the `'end'` event will not fire. Also, until
Nov 13, 2015
Nov 13, 2015
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
Apr 24, 2018
Apr 24, 2018
406
added: v10.0.0
Feb 15, 2018
Feb 15, 2018
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
May 2, 2018
May 2, 2018
462
See also: [`request.setTimeout()`][].
Sep 20, 2017
Sep 20, 2017
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
Apr 16, 2018
Apr 16, 2018
474
event is not being listened for and the response status code is 101 Switching
475
Protocols, clients receiving an upgrade header will have their connections
476
closed.
Feb 10, 2011
Feb 10, 2011
477
Mar 8, 2017
Mar 8, 2017
478
A client server pair demonstrating how to listen for the `'upgrade'` event.
Feb 10, 2011
Feb 10, 2011
479
Jan 21, 2016
Jan 21, 2016
480
```js
481
const http = require('http');
Feb 10, 2011
Feb 10, 2011
482
Jan 21, 2016
Jan 21, 2016
483
// Create an HTTP server
Apr 24, 2017
Apr 24, 2017
484
const srv = http.createServer((req, res) => {
Jun 2, 2017
Jun 2, 2017
485
res.writeHead(200, { 'Content-Type': 'text/plain' });
Jan 21, 2016
Jan 21, 2016
486
res.end('okay');
487
});
488
srv.on('upgrade', (req, socket, head) => {
489
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
490
'Upgrade: WebSocket\r\n' +
491
'Connection: Upgrade\r\n' +
492
'\r\n');
493
494
socket.pipe(socket); // echo back
495
});
496
497
// now that server is running
498
srv.listen(1337, '127.0.0.1', () => {
499
500
// make a request
Apr 4, 2017
Apr 4, 2017
501
const options = {
Jan 21, 2016
Jan 21, 2016
502
port: 1337,
503
hostname: '127.0.0.1',
504
headers: {
505
'Connection': 'Upgrade',
506
'Upgrade': 'websocket'
507
}
508
};
509
Apr 4, 2017
Apr 4, 2017
510
const req = http.request(options);
Jan 21, 2016
Jan 21, 2016
511
req.end();
512
513
req.on('upgrade', (res, socket, upgradeHead) => {
514
console.log('got upgraded!');
515
socket.end();
516
process.exit(0);
517
});
518
});
519
```
Feb 15, 2012
Feb 15, 2012
520
Nov 13, 2015
Nov 13, 2015
521
### request.abort()
Jun 29, 2016
Jun 29, 2016
522
<!-- YAML
523
added: v0.3.8
524
-->
Feb 15, 2012
Feb 15, 2012
525
Nov 13, 2015
Nov 13, 2015
526
Marks the request as aborting. Calling this will cause remaining data
527
in the response to be dropped and the socket to be destroyed.
Feb 10, 2011
Feb 10, 2011
528
Feb 27, 2017
Feb 27, 2017
529
### request.aborted
530
<!-- YAML
531
added: v0.11.14
May 14, 2018
May 14, 2018
532
changes:
533
- version: REPLACEME
534
pr-url: https://github.com/nodejs/node/pull/20230
535
description: The `aborted` property is no longer a timestamp number.
Feb 27, 2017
Feb 27, 2017
536
-->
537
May 14, 2018
May 14, 2018
538
* {boolean}
539
540
The `request.aborted` property will be `true` if the request has
541
been aborted.
Feb 27, 2017
Feb 27, 2017
542
Jun 19, 2017
Jun 19, 2017
543
### request.connection
544
<!-- YAML
545
added: v0.3.0
546
-->
547
548
* {net.Socket}
549
May 2, 2018
May 2, 2018
550
See [`request.socket`][].
Jun 19, 2017
Jun 19, 2017
551
Jun 13, 2017
Jun 13, 2017
552
### request.end([data[, encoding]][, callback])
Jun 29, 2016
Jun 29, 2016
553
<!-- YAML
554
added: v0.1.90
Feb 19, 2018
Feb 19, 2018
555
changes:
Apr 24, 2018
Apr 24, 2018
556
- version: v10.0.0
Feb 19, 2018
Feb 19, 2018
557
pr-url: https://github.com/nodejs/node/pull/18780
558
description: This method now returns a reference to `ClientRequest`.
Jun 29, 2016
Jun 29, 2016
559
-->
Feb 10, 2011
Feb 10, 2011
560
Mar 8, 2017
Mar 8, 2017
561
* `data` {string|Buffer}
Mar 2, 2017
Mar 2, 2017
562
* `encoding` {string}
Oct 11, 2016
Oct 11, 2016
563
* `callback` {Function}
Feb 19, 2018
Feb 19, 2018
564
* Returns: {this}
Oct 11, 2016
Oct 11, 2016
565
Nov 13, 2015
Nov 13, 2015
566
Finishes sending the request. If any parts of the body are
567
unsent, it will flush them to the stream. If the request is
568
chunked, this will send the terminating `'0\r\n\r\n'`.
Feb 10, 2011
Feb 10, 2011
569
Nov 13, 2015
Nov 13, 2015
570
If `data` is specified, it is equivalent to calling
Jul 9, 2017
Jul 9, 2017
571
[`request.write(data, encoding)`][] followed by `request.end(callback)`.
Feb 10, 2011
Feb 10, 2011
572
Nov 13, 2015
Nov 13, 2015
573
If `callback` is specified, it will be called when the request stream
574
is finished.
Feb 10, 2011
Feb 10, 2011
575
Nov 13, 2015
Nov 13, 2015
576
### request.flushHeaders()
Jun 29, 2016
Jun 29, 2016
577
<!-- YAML
578
added: v1.6.0
579
-->
Feb 10, 2011
Feb 10, 2011
580
Nov 13, 2015
Nov 13, 2015
581
Flush the request headers.
Feb 10, 2011
Feb 10, 2011
582
Mar 8, 2017
Mar 8, 2017
583
For efficiency reasons, Node.js normally buffers the request headers until
584
`request.end()` is called or the first chunk of request data is written. It
585
then tries to pack the request headers and data into a single TCP packet.
Feb 10, 2011
Feb 10, 2011
586
Mar 8, 2017
Mar 8, 2017
587
That's usually desired (it saves a TCP round-trip), but not when the first
Apr 4, 2018
Apr 4, 2018
588
data is not sent until possibly much later. `request.flushHeaders()` bypasses
Mar 8, 2017
Mar 8, 2017
589
the optimization and kickstarts the request.
Feb 10, 2011
Feb 10, 2011
590
Sep 15, 2017
Sep 15, 2017
591
### request.getHeader(name)
592
<!-- YAML
593
added: v1.6.0
594
-->
595
596
* `name` {string}
Apr 13, 2018
Apr 13, 2018
597
* Returns: {any}
Sep 15, 2017
Sep 15, 2017
598
599
Reads out a header on the request. Note that the name is case insensitive.
Apr 13, 2018
Apr 13, 2018
600
The type of the return value depends on the arguments provided to
601
[`request.setHeader()`][].
Sep 15, 2017
Sep 15, 2017
602
603
Example:
604
```js
Apr 13, 2018
Apr 13, 2018
605
request.setHeader('content-type', 'text/html');
606
request.setHeader('Content-Length', Buffer.byteLength(body));
607
request.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
Sep 15, 2017
Sep 15, 2017
608
const contentType = request.getHeader('Content-Type');
Apr 13, 2018
Apr 13, 2018
609
// contentType is 'text/html'
610
const contentLength = request.getHeader('Content-Length');
611
// contentLength is of type number
612
const setCookie = request.getHeader('set-cookie');
613
// setCookie is of type string[]
Sep 15, 2017
Sep 15, 2017
614
```
615
May 3, 2018
May 3, 2018
616
### request.maxHeadersCount
617
618
* {number} **Default:** `2000`
619
620
Limits maximum response headers count. If set to 0, no limit will be applied.
621
Sep 15, 2017
Sep 15, 2017
622
### request.removeHeader(name)
623
<!-- YAML
624
added: v1.6.0
625
-->
626
627
* `name` {string}
628
629
Removes a header that's already defined into headers object.
630
631
Example:
632
```js
633
request.removeHeader('Content-Type');
634
```
635
636
### request.setHeader(name, value)
637
<!-- YAML
638
added: v1.6.0
639
-->
640
641
* `name` {string}
Apr 13, 2018
Apr 13, 2018
642
* `value` {any}
Sep 15, 2017
Sep 15, 2017
643
644
Sets a single header value for headers object. If this header already exists in
645
the to-be-sent headers, its value will be replaced. Use an array of strings
Apr 13, 2018
Apr 13, 2018
646
here to send multiple headers with the same name. Non-string values will be
647
stored without modification. Therefore, [`request.getHeader()`][] may return
648
non-string values. However, the non-string values will be converted to strings
649
for network transmission.
Sep 15, 2017
Sep 15, 2017
650
651
Example:
652
```js
653
request.setHeader('Content-Type', 'application/json');
654
```
655
656
or
657
658
```js
659
request.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
660
```
661
Nov 13, 2015
Nov 13, 2015
662
### request.setNoDelay([noDelay])
Jun 29, 2016
Jun 29, 2016
663
<!-- YAML
664
added: v0.5.9
665
-->
Feb 10, 2011
Feb 10, 2011
666
Mar 2, 2017
Mar 2, 2017
667
* `noDelay` {boolean}
Oct 11, 2016
Oct 11, 2016
668
Nov 13, 2015
Nov 13, 2015
669
Once a socket is assigned to this request and is connected
Dec 3, 2015
Dec 3, 2015
670
[`socket.setNoDelay()`][] will be called.
Oct 28, 2010
Oct 28, 2010
671
Nov 13, 2015
Nov 13, 2015
672
### request.setSocketKeepAlive([enable][, initialDelay])
Jun 29, 2016
Jun 29, 2016
673
<!-- YAML
674
added: v0.5.9
675
-->
Feb 10, 2011
Feb 10, 2011
676
Mar 2, 2017
Mar 2, 2017
677
* `enable` {boolean}
678
* `initialDelay` {number}
Oct 11, 2016
Oct 11, 2016
679
Nov 13, 2015
Nov 13, 2015
680
Once a socket is assigned to this request and is connected
Dec 3, 2015
Dec 3, 2015
681
[`socket.setKeepAlive()`][] will be called.
Oct 28, 2010
Oct 28, 2010
682
Nov 13, 2015
Nov 13, 2015
683
### request.setTimeout(timeout[, callback])
Jun 29, 2016
Jun 29, 2016
684
<!-- YAML
685
added: v0.5.9
686
-->
Oct 28, 2010
Oct 28, 2010
687
Jan 18, 2018
Jan 18, 2018
688
* `timeout` {number} Milliseconds before a request times out.
Feb 23, 2018
Feb 23, 2018
689
* `callback` {Function} Optional function to be called when a timeout occurs.
Apr 12, 2018
Apr 12, 2018
690
Same as binding to the `'timeout'` event.
Apr 12, 2018
Apr 12, 2018
691
* Returns: {http.ClientRequest}
Dec 30, 2015
Dec 30, 2015
692
Oct 11, 2016
Oct 11, 2016
693
Once a socket is assigned to this request and is connected
694
[`socket.setTimeout()`][] will be called.
695
Jun 19, 2017
Jun 19, 2017
696
### request.socket
697
<!-- YAML
698
added: v0.3.0
699
-->
700
701
* {net.Socket}
702
703
Reference to the underlying socket. Usually users will not want to access
704
this property. In particular, the socket will not emit `'readable'` events
Apr 13, 2018
Apr 13, 2018
705
because of how the protocol parser attaches to the socket. The `socket`
706
may also be accessed via `request.connection`.
Jun 19, 2017
Jun 19, 2017
707
708
Example:
709
710
```js
711
const http = require('http');
Aug 18, 2017
Aug 18, 2017
712
const options = {
713
host: 'www.google.com',
714
};
715
const req = http.get(options);
716
req.end();
717
req.once('response', (res) => {
718
const ip = req.socket.localAddress;
719
const port = req.socket.localPort;
720
console.log(`Your IP address is ${ip} and your source port is ${port}.`);
721
// consume response object
722
});
Jun 19, 2017
Jun 19, 2017
723
```
724
Nov 13, 2015
Nov 13, 2015
725
### request.write(chunk[, encoding][, callback])
Jun 29, 2016
Jun 29, 2016
726
<!-- YAML
727
added: v0.1.29
728
-->
Oct 28, 2010
Oct 28, 2010
729
Mar 8, 2017
Mar 8, 2017
730
* `chunk` {string|Buffer}
Mar 2, 2017
Mar 2, 2017
731
* `encoding` {string}
Oct 11, 2016
Oct 11, 2016
732
* `callback` {Function}
Apr 12, 2018
Apr 12, 2018
733
* Returns: {boolean}
Oct 11, 2016
Oct 11, 2016
734
Apr 4, 2018
Apr 4, 2018
735
Sends a chunk of the body. By calling this method
Mar 8, 2017
Mar 8, 2017
736
many times, a request body can be sent to a
Apr 4, 2018
Apr 4, 2018
737
server β€” in that case it is suggested to use the
Nov 13, 2015
Nov 13, 2015
738
`['Transfer-Encoding', 'chunked']` header line when
739
creating the request.
Jul 31, 2012
Jul 31, 2012
740
Nov 13, 2015
Nov 13, 2015
741
The `encoding` argument is optional and only applies when `chunk` is a string.
742
Defaults to `'utf8'`.
Oct 28, 2010
Oct 28, 2010
743
Nov 13, 2015
Nov 13, 2015
744
The `callback` argument is optional and will be called when this chunk of data
745
is flushed.
Oct 28, 2010
Oct 28, 2010
746
Feb 6, 2018
Feb 6, 2018
747
Returns `true` if the entire data was flushed successfully to the kernel
748
buffer. Returns `false` if all or part of the data was queued in user memory.
749
`'drain'` will be emitted when the buffer is free again.
Oct 28, 2010
Oct 28, 2010
750
Nov 13, 2015
Nov 13, 2015
751
## Class: http.Server
Jun 29, 2016
Jun 29, 2016
752
<!-- YAML
753
added: v0.1.17
754
-->
Oct 28, 2010
Oct 28, 2010
755
Feb 23, 2018
Feb 23, 2018
756
This class inherits from [`net.Server`][] and has the following additional
757
events:
Oct 28, 2010
Oct 28, 2010
758
Nov 13, 2015
Nov 13, 2015
759
### Event: 'checkContinue'
Jun 29, 2016
Jun 29, 2016
760
<!-- YAML
761
added: v0.3.0
762
-->
Oct 28, 2010
Oct 28, 2010
763
Oct 11, 2016
Oct 11, 2016
764
* `request` {http.IncomingMessage}
765
* `response` {http.ServerResponse}
Oct 28, 2010
Oct 28, 2010
766
Oct 11, 2016
Oct 11, 2016
767
Emitted each time a request with an HTTP `Expect: 100-continue` is received.
Jan 27, 2017
Jan 27, 2017
768
If this event is not listened for, the server will automatically respond
Oct 11, 2016
Oct 11, 2016
769
with a `100 Continue` as appropriate.
Oct 28, 2010
Oct 28, 2010
770
Feb 23, 2018
Feb 23, 2018
771
Handling this event involves calling [`response.writeContinue()`][] if the
772
client should continue to send the request body, or generating an appropriate
773
HTTP response (e.g. 400 Bad Request) if the client should not continue to send
774
the request body.
Oct 28, 2010
Oct 28, 2010
775
Oct 11, 2016
Oct 11, 2016
776
Note that when this event is emitted and handled, the [`'request'`][] event will
777
not be emitted.
778
779
### Event: 'checkExpectation'
780
<!-- YAML
781
added: v5.5.0
782
-->
783
Aug 18, 2017
Aug 18, 2017
784
* `request` {http.IncomingMessage}
Oct 11, 2016
Oct 11, 2016
785
* `response` {http.ServerResponse}
786
787
Emitted each time a request with an HTTP `Expect` header is received, where the
Jan 27, 2017
Jan 27, 2017
788
value is not `100-continue`. If this event is not listened for, the server will
Oct 11, 2016
Oct 11, 2016
789
automatically respond with a `417 Expectation Failed` as appropriate.
790
791
Note that when this event is emitted and handled, the [`'request'`][] event will
Nov 13, 2015
Nov 13, 2015
792
not be emitted.
Aug 23, 2015
Aug 23, 2015
793
Nov 13, 2015
Nov 13, 2015
794
### Event: 'clientError'
Jun 29, 2016
Jun 29, 2016
795
<!-- YAML
796
added: v0.1.94
Feb 24, 2017
Feb 24, 2017
797
changes:
798
- version: v6.0.0
799
pr-url: https://github.com/nodejs/node/pull/4557
800
description: The default action of calling `.destroy()` on the `socket`
801
will no longer take place if there are listeners attached
Apr 12, 2018
Apr 12, 2018
802
for `'clientError'`.
Jan 10, 2018
Jan 10, 2018
803
- version: v9.4.0
Dec 27, 2017
Dec 27, 2017
804
pr-url: https://github.com/nodejs/node/pull/17672
May 2, 2018
May 2, 2018
805
description: The `rawPacket` is the current buffer that just parsed. Adding
Apr 12, 2018
Apr 12, 2018
806
this buffer to the error object of `'clientError'` event is to
807
make it possible that developers can log the broken packet.
Jun 29, 2016
Jun 29, 2016
808
-->
Aug 23, 2015
Aug 23, 2015
809
Oct 11, 2016
Oct 11, 2016
810
* `exception` {Error}
811
* `socket` {net.Socket}
Oct 28, 2010
Oct 28, 2010
812
Dec 3, 2015
Dec 3, 2015
813
If a client connection emits an `'error'` event, it will be forwarded here.
Jan 7, 2016
Jan 7, 2016
814
Listener of this event is responsible for closing/destroying the underlying
Feb 23, 2018
Feb 23, 2018
815
socket. For example, one may wish to more gracefully close the socket with a
816
custom HTTP response instead of abruptly severing the connection.
Jan 7, 2016
Jan 7, 2016
817
Feb 23, 2018
Feb 23, 2018
818
Default behavior is to close the socket with an HTTP '400 Bad Request' response
819
if possible, otherwise the socket is immediately destroyed.
May 16, 2012
May 16, 2012
820
Dec 3, 2015
Dec 3, 2015
821
`socket` is the [`net.Socket`][] object that the error originated from.
Oct 28, 2010
Oct 28, 2010
822
Feb 17, 2016
Feb 17, 2016
823
```js
824
const http = require('http');
825
826
const server = http.createServer((req, res) => {
827
res.end();
828
});
829
server.on('clientError', (err, socket) => {
830
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
831
});
832
server.listen(8000);
833
```
834
835
When the `'clientError'` event occurs, there is no `request` or `response`
836
object, so any HTTP response sent, including response headers and payload,
837
*must* be written directly to the `socket` object. Care must be taken to
838
ensure the response is a properly formatted HTTP response message.
839
Dec 27, 2017
Dec 27, 2017
840
`err` is an instance of `Error` with two extra columns:
841
842
+ `bytesParsed`: the bytes count of request packet that Node.js may have parsed
843
correctly;
844
+ `rawPacket`: the raw packet of current request.
845
Nov 13, 2015
Nov 13, 2015
846
### Event: 'close'
Jun 29, 2016
Jun 29, 2016
847
<!-- YAML
848
added: v0.1.4
849
-->
Oct 28, 2010
Oct 28, 2010
850
Nov 13, 2015
Nov 13, 2015
851
Emitted when the server closes.
Nov 28, 2013
Nov 28, 2013
852
Nov 13, 2015
Nov 13, 2015
853
### Event: 'connect'
Jun 29, 2016
Jun 29, 2016
854
<!-- YAML
855
added: v0.7.0
856
-->
Jan 21, 2011
Jan 21, 2011
857
Oct 11, 2016
Oct 11, 2016
858
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
859
the [`'request'`][] event
860
* `socket` {net.Socket} Network socket between the server and client
861
* `head` {Buffer} The first packet of the tunneling stream (may be empty)
Jan 21, 2011
Jan 21, 2011
862
Jan 27, 2017
Jan 27, 2017
863
Emitted each time a client requests an HTTP `CONNECT` method. If this event is
864
not listened for, then clients requesting a `CONNECT` method will have their
Nov 13, 2015
Nov 13, 2015
865
connections closed.
Sep 17, 2014
Sep 17, 2014
866
Dec 3, 2015
Dec 3, 2015
867
After this event is emitted, the request's socket will not have a `'data'`
Mar 8, 2017
Mar 8, 2017
868
event listener, meaning it will need to be bound in order to handle data
Nov 13, 2015
Nov 13, 2015
869
sent to the server on that socket.
Oct 28, 2010
Oct 28, 2010
870
Nov 13, 2015
Nov 13, 2015
871
### Event: 'connection'
Jun 29, 2016
Jun 29, 2016
872
<!-- YAML
873
added: v0.1.0
874
-->
May 5, 2011
May 5, 2011
875
Oct 11, 2016
Oct 11, 2016
876
* `socket` {net.Socket}
Jan 21, 2011
Jan 21, 2011
877
Oct 23, 2017
Oct 23, 2017
878
This event is emitted when a new TCP stream is established. `socket` is
879
typically an object of type [`net.Socket`][]. Usually users will not want to
880
access this event. In particular, the socket will not emit `'readable'` events
881
because of how the protocol parser attaches to the socket. The `socket` can
882
also be accessed at `request.connection`.
883
Feb 8, 2018
Feb 8, 2018
884
This event can also be explicitly emitted by users to inject connections
Oct 23, 2017
Oct 23, 2017
885
into the HTTP server. In that case, any [`Duplex`][] stream can be passed.
Oct 28, 2010
Oct 28, 2010
886
Nov 13, 2015
Nov 13, 2015
887
### Event: 'request'
Jun 29, 2016
Jun 29, 2016
888
<!-- YAML
889
added: v0.1.0
890
-->
Jan 21, 2011
Jan 21, 2011
891
Oct 11, 2016
Oct 11, 2016
892
* `request` {http.IncomingMessage}
893
* `response` {http.ServerResponse}
Oct 28, 2010
Oct 28, 2010
894
Nov 13, 2015
Nov 13, 2015
895
Emitted each time there is a request. Note that there may be multiple requests
Jan 25, 2017
Jan 25, 2017
896
per connection (in the case of HTTP Keep-Alive connections).
Oct 28, 2010
Oct 28, 2010
897
Nov 13, 2015
Nov 13, 2015
898
### Event: 'upgrade'
Jun 29, 2016
Jun 29, 2016
899
<!-- YAML
900
added: v0.1.94
Apr 16, 2018
Apr 16, 2018
901
changes:
Apr 24, 2018
Apr 24, 2018
902
- version: v10.0.0
903
pr-url: v10.0.0
Apr 16, 2018
Apr 16, 2018
904
description: Not listening to this event no longer causes the socket
905
to be destroyed if a client sends an Upgrade header.
Jun 29, 2016
Jun 29, 2016
906
-->
Oct 28, 2010
Oct 28, 2010
907
Oct 11, 2016
Oct 11, 2016
908
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
909
the [`'request'`][] event
910
* `socket` {net.Socket} Network socket between the server and client
911
* `head` {Buffer} The first packet of the upgraded stream (may be empty)
Oct 28, 2010
Oct 28, 2010
912
Apr 16, 2018
Apr 16, 2018
913
Emitted each time a client requests an HTTP upgrade. Listening to this event
914
is optional and clients cannot insist on a protocol change.
Oct 22, 2011
Oct 22, 2011
915
Dec 3, 2015
Dec 3, 2015
916
After this event is emitted, the request's socket will not have a `'data'`
Mar 8, 2017
Mar 8, 2017
917
event listener, meaning it will need to be bound in order to handle data
Nov 13, 2015
Nov 13, 2015
918
sent to the server on that socket.
Jan 21, 2011
Jan 21, 2011
919
Nov 13, 2015
Nov 13, 2015
920
### server.close([callback])
Jun 29, 2016
Jun 29, 2016
921
<!-- YAML
922
added: v0.1.90
923
-->
Jan 21, 2011
Jan 21, 2011
924
Oct 11, 2016
Oct 11, 2016
925
* `callback` {Function}
926
Apr 4, 2018
Apr 4, 2018
927
Stops the server from accepting new connections. See [`net.Server.close()`][].
Jan 21, 2011
Jan 21, 2011
928
Oct 19, 2017
Oct 19, 2017
929
### server.listen()
Aug 29, 2016
Aug 29, 2016
930
Oct 19, 2017
Oct 19, 2017
931
Starts the HTTP server listening for connections.
932
This method is identical to [`server.listen()`][] from [`net.Server`][].
Aug 15, 2013
Aug 15, 2013
933
Jan 21, 2016
Jan 21, 2016
934
### server.listening
Jun 29, 2016
Jun 29, 2016
935
<!-- YAML
936
added: v5.7.0
937
-->
Jan 21, 2016
Jan 21, 2016
938
May 2, 2018
May 2, 2018
939
* {boolean} Indicates whether or not the server is listening for connections.
Jan 21, 2016
Jan 21, 2016
940
Mar 27, 2017
Mar 27, 2017
941
### server.maxHeadersCount
Jun 29, 2016
Jun 29, 2016
942
<!-- YAML
943
added: v0.7.0
944
-->
Jul 26, 2011
Jul 26, 2011
945
Apr 4, 2018
Apr 4, 2018
946
* {number} **Default:** `2000`
Oct 11, 2016
Oct 11, 2016
947
May 3, 2018
May 3, 2018
948
Limits maximum incoming headers count. If set to 0, no limit will be applied.
Aug 15, 2013
Aug 15, 2013
949
Mar 8, 2017
Mar 8, 2017
950
### server.setTimeout([msecs][, callback])
Jun 29, 2016
Jun 29, 2016
951
<!-- YAML
952
added: v0.9.12
953
-->
Aug 15, 2013
Aug 15, 2013
954
Apr 4, 2018
Apr 4, 2018
955
* `msecs` {number} **Default:** `120000` (2 minutes)
Nov 13, 2015
Nov 13, 2015
956
* `callback` {Function}
Apr 12, 2018
Apr 12, 2018
957
* Returns: {http.Server}
Jul 26, 2011
Jul 26, 2011
958
Nov 13, 2015
Nov 13, 2015
959
Sets the timeout value for sockets, and emits a `'timeout'` event on
960
the Server object, passing the socket as an argument, if a timeout
961
occurs.
Jul 26, 2011
Jul 26, 2011
962
Nov 13, 2015
Nov 13, 2015
963
If there is a `'timeout'` event listener on the Server object, then it
964
will be called with the timed-out socket as an argument.
Aug 15, 2013
Aug 15, 2013
965
Nov 13, 2015
Nov 13, 2015
966
By default, the Server's timeout value is 2 minutes, and sockets are
Apr 4, 2018
Apr 4, 2018
967
destroyed automatically if they time out. However, if a callback is assigned
Mar 8, 2017
Mar 8, 2017
968
to the Server's `'timeout'` event, timeouts must be handled explicitly.
Aug 15, 2013
Aug 15, 2013
969
Mar 27, 2017
Mar 27, 2017
970
### server.timeout
Jun 29, 2016
Jun 29, 2016
971
<!-- YAML
972
added: v0.9.12
973
-->
Jul 26, 2011
Jul 26, 2011
974
Apr 4, 2018
Apr 4, 2018
975
* {number} Timeout in milliseconds. **Default:** `120000` (2 minutes).
Jul 26, 2011
Jul 26, 2011
976
Nov 13, 2015
Nov 13, 2015
977
The number of milliseconds of inactivity before a socket is presumed
978
to have timed out.
Aug 15, 2013
Aug 15, 2013
979
Dec 28, 2017
Dec 28, 2017
980
A value of `0` will disable the timeout behavior on incoming connections.
Aug 15, 2013
Aug 15, 2013
981
Feb 8, 2018
Feb 8, 2018
982
The socket timeout logic is set up on connection, so changing this
May 26, 2017
May 26, 2017
983
value only affects new connections to the server, not any existing connections.
984
985
### server.keepAliveTimeout
986
<!-- YAML
May 30, 2017
May 30, 2017
987
added: v8.0.0
May 26, 2017
May 26, 2017
988
-->
989
Apr 4, 2018
Apr 4, 2018
990
* {number} Timeout in milliseconds. **Default:** `5000` (5 seconds).
May 26, 2017
May 26, 2017
991
992
The number of milliseconds of inactivity a server needs to wait for additional
993
incoming data, after it has finished writing the last response, before a socket
994
will be destroyed. If the server receives new data before the keep-alive
995
timeout has fired, it will reset the regular inactivity timeout, i.e.,
996
[`server.timeout`][].
997
Feb 23, 2018
Feb 23, 2018
998
A value of `0` will disable the keep-alive timeout behavior on incoming
999
connections.
1000
A value of `0` makes the http server behave similarly to Node.js versions prior