UNPKG

14.1 kBMarkdownView Raw
1# Node Res
2
3> A facade over Node.js HTTP `res` object with no side-effects.
4
5[![NPM Version][npm-image]][npm-url]
6[![Build Status][travis-image]][travis-url]
7[![Appveyor][appveyor-image]][appveyor-url]
8[![Coveralls][coveralls-image]][coveralls-url]
9
10<a href="http://res.cloudinary.com/adonisjs/image/upload/q_100/v1502279403/poppinss_z8uk2j.png">
11<img src="http://res.cloudinary.com/adonisjs/image/upload/q_100/v1502279403/poppinss_z8uk2j.png" width="300px" align="right" vspace="20px" />
12</a>
13
14`node-res` is a simple module to make HTTP response in Node.js. It offers helpers to make it easier to set `headers`, define response statuses and properly parse response type to set appropriate headers.
15
16For example:
17
18```js
19// content-type: plain/text
20nodeRes.send(req, res, 'Hello world')
21
22// content-type: application/json
23nodeRes.send(req, res, { greeting: 'hello world' })
24
25// content-type: text/html
26nodeRes.send(req, res, '<h2> Hello world </h2>')
27```
28
29## See also
30
311. [node-req](https://npmjs.org/package/node-req)
322. [node-cookie](https://npmjs.org/package/node-cookie)
33
34## Basic Example
35
36```javascript
37const http = require('http')
38const nodeRes = require('node-res')
39
40http.createServer(function (req, res) {
41
42 // plain text
43 nodeRes.send(req, res, "Hello world")
44
45 // json
46 nodeRes.json(req, res, {time:"now"})
47
48 // jsonp
49 nodeRes.jsonp(req, res, {time:"now"}, "callback")
50
51}).listen(3000)
52
53```
54
55## API
56<a name="module_Response"></a>
57
58## Response
59A simple IO module to make consistent HTTP response, without
60worrying about underlying details.
61
62
63* [Response](#module_Response)
64 * [~getHeader(res, key)](#module_Response..getHeader) ⇒ <code>Array</code> \| <code>String</code>
65 * [~header(res, key, value)](#module_Response..header) ⇒ <code>void</code>
66 * [~append(res, key, value)](#module_Response..append) ⇒ <code>void</code>
67 * [~status(res, code)](#module_Response..status) ⇒ <code>void</code>
68 * [~safeHeader(res, key, value)](#module_Response..safeHeader) ⇒ <code>void</code>
69 * [~removeHeader(res, key)](#module_Response..removeHeader) ⇒ <code>void</code>
70 * [~write(res, body)](#module_Response..write) ⇒ <code>void</code>
71 * [~end(res, [payload])](#module_Response..end) ⇒ <code>void</code>
72 * [~send(req, res, body, [generateEtag])](#module_Response..send) ⇒ <code>void</code>
73 * [~etag(res, body)](#module_Response..etag) ⇒ <code>void</code>
74 * [~prepare(res, body)](#module_Response..prepare) ⇒ <code>String</code>
75 * [~prepareJsonp(res, body, callbackFn)](#module_Response..prepareJsonp) ⇒ <code>String</code>
76 * [~json(req, res, body, [generateEtag])](#module_Response..json) ⇒ <code>void</code>
77 * [~jsonp(req, res, body, [callbackFn], [generateEtag])](#module_Response..jsonp) ⇒ <code>void</code>
78 * [~location(res, url)](#module_Response..location) ⇒ <code>void</code>
79 * [~redirect(req, res, url, [status])](#module_Response..redirect) ⇒ <code>void</code>
80 * [~vary(res, field)](#module_Response..vary) ⇒ <code>void</code>
81 * [~type(req, res, [charset])](#module_Response..type) ⇒ <code>void</code>
82 * [~stream(res, body)](#module_Response..stream) ⇒ <code>Promise</code>
83
84<a name="module_Response..getHeader"></a>
85
86### Response~getHeader(res, key) ⇒ <code>Array</code> \| <code>String</code>
87Returns the value of an existing header on
88the response object
89
90**Kind**: inner method of [<code>Response</code>](#module_Response)
91**Returns**: <code>Array</code> \| <code>String</code> - Return type depends upon the header existing value
92
93| Param | Type |
94| --- | --- |
95| res | <code>ServerResponse</code> |
96| key | <code>String</code> |
97
98**Example**
99```js
100nodeRes.getHeader(res, 'Content-type')
101```
102<a name="module_Response..header"></a>
103
104### Response~header(res, key, value) ⇒ <code>void</code>
105Sets header on the response object. This method will wipe off
106existing values. To append to existing values, use `append`.
107
108**Kind**: inner method of [<code>Response</code>](#module_Response)
109
110| Param | Type |
111| --- | --- |
112| res | <code>http.ServerResponse</code> |
113| key | <code>String</code> |
114| value | <code>String</code> \| <code>Array</code> |
115
116**Example**
117```js
118nodeRes.header(res, 'Content-type', 'application/json')
119
120// or set an array of headers
121nodeRes.header(res, 'Link', ['<http://localhost/>', '<http://localhost:3000/>'])
122```
123<a name="module_Response..append"></a>
124
125### Response~append(res, key, value) ⇒ <code>void</code>
126Appends value to the header existing values.
127
128**Kind**: inner method of [<code>Response</code>](#module_Response)
129
130| Param | Type |
131| --- | --- |
132| res | <code>http.ServerResponse</code> |
133| key | <code>String</code> |
134| value | <code>String</code> \| <code>Array</code> |
135
136**Example**
137```js
138nodeRes.append(res, 'Content-type', 'application/json')
139
140// or append an array of headers
141nodeRes.append(res, 'Link', ['<http://localhost/>', '<http://localhost:3000/>'])
142```
143<a name="module_Response..status"></a>
144
145### Response~status(res, code) ⇒ <code>void</code>
146Set status on the HTTP res object
147
148**Kind**: inner method of [<code>Response</code>](#module_Response)
149
150| Param | Type |
151| --- | --- |
152| res | <code>http.ServerResponse</code> |
153| code | <code>Number</code> |
154
155**Example**
156```js
157nodeRes.status(res, 200)
158```
159<a name="module_Response..safeHeader"></a>
160
161### Response~safeHeader(res, key, value) ⇒ <code>void</code>
162Sets the header on response object, only if it
163does not exists.
164
165**Kind**: inner method of [<code>Response</code>](#module_Response)
166
167| Param | Type |
168| --- | --- |
169| res | <code>http.ServerResponse</code> |
170| key | <code>String</code> |
171| value | <code>String</code> \| <code>Array</code> |
172
173**Example**
174```js
175nodeRes.safeHeader(res, 'Content-type', 'application/json')
176```
177<a name="module_Response..removeHeader"></a>
178
179### Response~removeHeader(res, key) ⇒ <code>void</code>
180Removes the header from response
181
182**Kind**: inner method of [<code>Response</code>](#module_Response)
183
184| Param | Type |
185| --- | --- |
186| res | <code>http.ServerResponse</code> |
187| key | <code>String</code> |
188
189**Example**
190```js
191nodeRes.removeHeader(res, 'Content-type')
192```
193<a name="module_Response..write"></a>
194
195### Response~write(res, body) ⇒ <code>void</code>
196Write string or buffer to the response object.
197
198**Kind**: inner method of [<code>Response</code>](#module_Response)
199
200| Param | Type |
201| --- | --- |
202| res | <code>http.ServerResponse</code> |
203| body | <code>String</code> \| <code>Buffer</code> |
204
205**Example**
206```js
207nodeRes.write(res, 'Hello world')
208```
209<a name="module_Response..end"></a>
210
211### Response~end(res, [payload]) ⇒ <code>void</code>
212Explictly end HTTP response
213
214**Kind**: inner method of [<code>Response</code>](#module_Response)
215
216| Param | Type |
217| --- | --- |
218| res | <code>http.ServerResponse</code> |
219| [payload] | <code>String</code> \| <code>Buffer</code> |
220
221**Example**
222```js
223nodeRes.end(res, 'Hello world')
224```
225<a name="module_Response..send"></a>
226
227### Response~send(req, res, body, [generateEtag]) ⇒ <code>void</code>
228Send body as the HTTP response and end it. Also
229this method will set the appropriate `Content-type`
230and `Content-length`.
231
232If body is set to null, this method will end the response
233as 204.
234
235**Kind**: inner method of [<code>Response</code>](#module_Response)
236
237| Param | Type | Default |
238| --- | --- | --- |
239| req | <code>http.ServerRequest</code> | |
240| res | <code>http.ServerResponse</code> | |
241| body | <code>String</code> \| <code>Buffer</code> \| <code>Object</code> \| <code>Stream</code> | |
242| [generateEtag] | <code>Boolean</code> | <code>true</code> |
243
244**Example**
245```js
246nodeRes.send(req, res, 'Hello world')
247
248// or html
249nodeRes.send(req, res, '<h2> Hello world </h2>')
250
251// or JSON
252nodeRes.send(req, res, { greeting: 'Hello world' })
253
254// or Buffer
255nodeRes.send(req, res, Buffer.from('Hello world', 'utf-8'))
256
257// Ignore etag
258nodeRes.send(req, res, 'Hello world', false)
259```
260<a name="module_Response..etag"></a>
261
262### Response~etag(res, body) ⇒ <code>void</code>
263Sets the Etag header for a given body chunk
264
265**Kind**: inner method of [<code>Response</code>](#module_Response)
266
267| Param | Type |
268| --- | --- |
269| res | <code>http.ServerResponse</code> |
270| body | <code>String</code> \| <code>Buffer</code> |
271
272**Example**
273```js
274nodeRes.etag(res, 'Hello world')
275```
276<a name="module_Response..prepare"></a>
277
278### Response~prepare(res, body) ⇒ <code>String</code>
279Prepares the response body by encoding it properly. Also
280sets appropriate headers based upon the body content type.
281
282This method is used internally by `send`, so you should
283never use it when calling `send`.
284
285It is helpful when you want to get the final payload and end the
286response at a later stage.
287
288**Kind**: inner method of [<code>Response</code>](#module_Response)
289
290| Param | Type |
291| --- | --- |
292| res | <code>http.ServerResponse</code> |
293| body | <code>Mixed</code> |
294
295**Example**
296```js
297const chunk = nodeRes.prepare(res, '<h2> Hello </h2>')
298
299if (chunk) {
300 nodeRes.etag(res, chunk)
301
302 if (nodeReq.fresh(req, res)) {
303 chunk = null
304 nodeRes.status(304)
305 }
306
307 nodeRes.end(chunk)
308}
309```
310<a name="module_Response..prepareJsonp"></a>
311
312### Response~prepareJsonp(res, body, callbackFn) ⇒ <code>String</code>
313Prepares response for JSONP
314
315**Kind**: inner method of [<code>Response</code>](#module_Response)
316
317| Param | Type |
318| --- | --- |
319| res | <code>http.ServerResponse</code> |
320| body | <code>Object</code> |
321| callbackFn | <code>String</code> |
322
323**Example**
324```js
325const chunk = nodeRes.prepareJsonp(res, '<h2> Hello </h2>', 'callback')
326
327if (chunk) {
328 nodeRes.etag(res, chunk)
329
330 if (nodeReq.fresh(req, res)) {
331 chunk = null
332 nodeRes.status(304)
333 }
334
335 nodeRes.end(chunk)
336}
337```
338<a name="module_Response..json"></a>
339
340### Response~json(req, res, body, [generateEtag]) ⇒ <code>void</code>
341Returns the HTTP response with `Content-type`
342set to `application/json`.
343
344**Kind**: inner method of [<code>Response</code>](#module_Response)
345
346| Param | Type | Default |
347| --- | --- | --- |
348| req | <code>http.IncomingMessage</code> | |
349| res | <code>http.ServerResponse</code> | |
350| body | <code>Object</code> | |
351| [generateEtag] | <code>Boolean</code> | <code>true</code> |
352
353**Example**
354```js
355nodeRes.json(req, res, { name: 'virk' })
356nodeRes.json(req, res, [ 'virk', 'joe' ])
357```
358<a name="module_Response..jsonp"></a>
359
360### Response~jsonp(req, res, body, [callbackFn], [generateEtag]) ⇒ <code>void</code>
361Make JSONP response with `Content-type` set to
362`text/javascript`.
363
364**Kind**: inner method of [<code>Response</code>](#module_Response)
365
366| Param | Type | Default |
367| --- | --- | --- |
368| req | <code>http.IncomingMessage</code> | |
369| res | <code>http.ServerResponse</code> | |
370| body | <code>Object</code> | |
371| [callbackFn] | <code>String</code> | <code>&#x27;callback&#x27;</code> |
372| [generateEtag] | <code>Boolean</code> | <code>true</code> |
373
374**Example**
375```js
376nodeRes.jsonp(req, res, { name: 'virk' }, 'callback')
377```
378<a name="module_Response..location"></a>
379
380### Response~location(res, url) ⇒ <code>void</code>
381Set `Location` header on the HTTP response.
382
383**Kind**: inner method of [<code>Response</code>](#module_Response)
384
385| Param | Type |
386| --- | --- |
387| res | <code>http.ServerResponse</code> |
388| url | <code>String</code> |
389
390<a name="module_Response..redirect"></a>
391
392### Response~redirect(req, res, url, [status]) ⇒ <code>void</code>
393Redirect the HTTP request to the given url.
394
395**Kind**: inner method of [<code>Response</code>](#module_Response)
396
397| Param | Type | Default |
398| --- | --- | --- |
399| req | <code>http.IncomingMessage</code> | |
400| res | <code>http.ServerResponse</code> | |
401| url | <code>String</code> | |
402| [status] | <code>Number</code> | <code>302</code> |
403
404**Example**
405```js
406nodeRes.redirect(req, res, '/')
407```
408<a name="module_Response..vary"></a>
409
410### Response~vary(res, field) ⇒ <code>void</code>
411Add vary header to the HTTP response.
412
413**Kind**: inner method of [<code>Response</code>](#module_Response)
414
415| Param | Type |
416| --- | --- |
417| res | <code>http.ServerResponse</code> |
418| field | <code>String</code> |
419
420<a name="module_Response..type"></a>
421
422### Response~type(req, res, [charset]) ⇒ <code>void</code>
423Set content type header by looking up the actual
424type and setting charset to utf8.
425
426### Note
427When defining custom charset, you must set pass the complete
428content type, otherwise `false` will be set as the
429content-type header.
430
431**Kind**: inner method of [<code>Response</code>](#module_Response)
432
433| Param | Type |
434| --- | --- |
435| req | <code>http.IncomingMessage</code> |
436| res | <code>http.ServerResponse</code> |
437| [charset] | <code>String</code> |
438
439**Example**
440```js
441nodeRes.type(res, 'html')
442
443nodeRes.type(res, 'json')
444
445nodeRes.type(res, 'text/html', 'ascii')
446```
447<a name="module_Response..stream"></a>
448
449### Response~stream(res, body) ⇒ <code>Promise</code>
450Pipe stream to the response. Also this method will make sure
451to destroy the stream, if request gets cancelled.
452
453The promise resolve when response finishes and rejects, when
454stream raises errors.
455
456**Kind**: inner method of [<code>Response</code>](#module_Response)
457
458| Param | Type |
459| --- | --- |
460| res | <code>Object</code> |
461| body | <code>Stream</code> |
462
463**Example**
464```js
465Response.stream(res, fs.createReadStream('foo.txt'))
466
467// handle stream errors
468Response
469 .stream(res, fs.createReadStream('foo.txt'))
470 .catch((error) => {
471 })
472```
473
474[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/node-res/master.svg?style=flat-square
475[appveyor-url]: https://ci.appveyor.com/project/thetutlage/node-res
476
477[npm-image]: https://img.shields.io/npm/v/node-res.svg?style=flat-square
478[npm-url]: https://npmjs.org/package/node-res
479
480[travis-image]: https://img.shields.io/travis/poppinss/node-res/master.svg?style=flat-square
481[travis-url]: https://travis-ci.org/poppinss/node-res
482
483[coveralls-image]: https://img.shields.io/coveralls/poppinss/node-res/develop.svg?style=flat-square
484[coveralls-url]: https://coveralls.io/github/poppinss/node-res