# Node Res > A facade over Node.js HTTP `res` object with no side-effects. [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Appveyor][appveyor-image]][appveyor-url] [![Coveralls][coveralls-image]][coveralls-url] `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. For example: ```js // content-type: plain/text nodeRes.send(req, res, 'Hello world') // content-type: application/json nodeRes.send(req, res, { greeting: 'hello world' }) // content-type: text/html nodeRes.send(req, res, '
Array
\| String
* [~header(res, key, value)](#module_Response..header) ⇒ void
* [~append(res, key, value)](#module_Response..append) ⇒ void
* [~status(res, code)](#module_Response..status) ⇒ void
* [~safeHeader(res, key, value)](#module_Response..safeHeader) ⇒ void
* [~removeHeader(res, key)](#module_Response..removeHeader) ⇒ void
* [~write(res, body)](#module_Response..write) ⇒ void
* [~end(res, [payload])](#module_Response..end) ⇒ void
* [~send(req, res, body, [generateEtag])](#module_Response..send) ⇒ void
* [~etag(res, body)](#module_Response..etag) ⇒ void
* [~prepare(res, body)](#module_Response..prepare) ⇒ String
* [~prepareJsonp(res, body, callbackFn)](#module_Response..prepareJsonp) ⇒ String
* [~json(req, res, body, [generateEtag])](#module_Response..json) ⇒ void
* [~jsonp(req, res, body, [callbackFn], [generateEtag])](#module_Response..jsonp) ⇒ void
* [~location(res, url)](#module_Response..location) ⇒ void
* [~redirect(req, res, url, [status])](#module_Response..redirect) ⇒ void
* [~vary(res, field)](#module_Response..vary) ⇒ void
* [~type(req, res, [charset])](#module_Response..type) ⇒ void
* [~stream(res, body)](#module_Response..stream) ⇒ Promise
### Response~getHeader(res, key) ⇒ Array
\| String
Returns the value of an existing header on
the response object
**Kind**: inner method of [Response
](#module_Response)
**Returns**: Array
\| String
- Return type depends upon the header existing value
| Param | Type |
| --- | --- |
| res | ServerResponse
|
| key | String
|
**Example**
```js
nodeRes.getHeader(res, 'Content-type')
```
### Response~header(res, key, value) ⇒ void
Sets header on the response object. This method will wipe off
existing values. To append to existing values, use `append`.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| key | String
|
| value | String
\| Array
|
**Example**
```js
nodeRes.header(res, 'Content-type', 'application/json')
// or set an array of headers
nodeRes.header(res, 'Link', ['void
Appends value to the header existing values.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| key | String
|
| value | String
\| Array
|
**Example**
```js
nodeRes.append(res, 'Content-type', 'application/json')
// or append an array of headers
nodeRes.append(res, 'Link', ['void
Set status on the HTTP res object
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| code | Number
|
**Example**
```js
nodeRes.status(res, 200)
```
### Response~safeHeader(res, key, value) ⇒ void
Sets the header on response object, only if it
does not exists.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| key | String
|
| value | String
\| Array
|
**Example**
```js
nodeRes.safeHeader(res, 'Content-type', 'application/json')
```
### Response~removeHeader(res, key) ⇒ void
Removes the header from response
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| key | String
|
**Example**
```js
nodeRes.removeHeader(res, 'Content-type')
```
### Response~write(res, body) ⇒ void
Write string or buffer to the response object.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| body | String
\| Buffer
|
**Example**
```js
nodeRes.write(res, 'Hello world')
```
### Response~end(res, [payload]) ⇒ void
Explictly end HTTP response
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| [payload] | String
\| Buffer
|
**Example**
```js
nodeRes.end(res, 'Hello world')
```
### Response~send(req, res, body, [generateEtag]) ⇒ void
Send body as the HTTP response and end it. Also
this method will set the appropriate `Content-type`
and `Content-length`.
If body is set to null, this method will end the response
as 204.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type | Default |
| --- | --- | --- |
| req | http.ServerRequest
| |
| res | http.ServerResponse
| |
| body | String
\| Buffer
\| Object
\| Stream
| |
| [generateEtag] | Boolean
| true
|
**Example**
```js
nodeRes.send(req, res, 'Hello world')
// or html
nodeRes.send(req, res, 'void
Sets the Etag header for a given body chunk
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| body | String
\| Buffer
|
**Example**
```js
nodeRes.etag(res, 'Hello world')
```
### Response~prepare(res, body) ⇒ String
Prepares the response body by encoding it properly. Also
sets appropriate headers based upon the body content type.
This method is used internally by `send`, so you should
never use it when calling `send`.
It is helpful when you want to get the final payload and end the
response at a later stage.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| body | Mixed
|
**Example**
```js
const chunk = nodeRes.prepare(res, 'String
Prepares response for JSONP
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| body | Object
|
| callbackFn | String
|
**Example**
```js
const chunk = nodeRes.prepareJsonp(res, 'void
Returns the HTTP response with `Content-type`
set to `application/json`.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type | Default |
| --- | --- | --- |
| req | http.IncomingMessage
| |
| res | http.ServerResponse
| |
| body | Object
| |
| [generateEtag] | Boolean
| true
|
**Example**
```js
nodeRes.json(req, res, { name: 'virk' })
nodeRes.json(req, res, [ 'virk', 'joe' ])
```
### Response~jsonp(req, res, body, [callbackFn], [generateEtag]) ⇒ void
Make JSONP response with `Content-type` set to
`text/javascript`.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type | Default |
| --- | --- | --- |
| req | http.IncomingMessage
| |
| res | http.ServerResponse
| |
| body | Object
| |
| [callbackFn] | String
| 'callback'
|
| [generateEtag] | Boolean
| true
|
**Example**
```js
nodeRes.jsonp(req, res, { name: 'virk' }, 'callback')
```
### Response~location(res, url) ⇒ void
Set `Location` header on the HTTP response.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| url | String
|
### Response~redirect(req, res, url, [status]) ⇒ void
Redirect the HTTP request to the given url.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type | Default |
| --- | --- | --- |
| req | http.IncomingMessage
| |
| res | http.ServerResponse
| |
| url | String
| |
| [status] | Number
| 302
|
**Example**
```js
nodeRes.redirect(req, res, '/')
```
### Response~vary(res, field) ⇒ void
Add vary header to the HTTP response.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | http.ServerResponse
|
| field | String
|
### Response~type(req, res, [charset]) ⇒ void
Set content type header by looking up the actual
type and setting charset to utf8.
### Note
When defining custom charset, you must set pass the complete
content type, otherwise `false` will be set as the
content-type header.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| req | http.IncomingMessage
|
| res | http.ServerResponse
|
| [charset] | String
|
**Example**
```js
nodeRes.type(res, 'html')
nodeRes.type(res, 'json')
nodeRes.type(res, 'text/html', 'ascii')
```
### Response~stream(res, body) ⇒ Promise
Pipe stream to the response. Also this method will make sure
to destroy the stream, if request gets cancelled.
The promise resolve when response finishes and rejects, when
stream raises errors.
**Kind**: inner method of [Response
](#module_Response)
| Param | Type |
| --- | --- |
| res | Object
|
| body | Stream
|
**Example**
```js
Response.stream(res, fs.createReadStream('foo.txt'))
// handle stream errors
Response
.stream(res, fs.createReadStream('foo.txt'))
.catch((error) => {
})
```
[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/node-res/master.svg?style=flat-square
[appveyor-url]: https://ci.appveyor.com/project/thetutlage/node-res
[npm-image]: https://img.shields.io/npm/v/node-res.svg?style=flat-square
[npm-url]: https://npmjs.org/package/node-res
[travis-image]: https://img.shields.io/travis/poppinss/node-res/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/poppinss/node-res
[coveralls-image]: https://img.shields.io/coveralls/poppinss/node-res/develop.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/poppinss/node-res