Node Res
A facade over Node.js HTTP
res
object with no side-effects.
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:
// content-type: plain/textnodeRes // content-type: application/jsonnodeRes // content-type: text/htmlnodeRes
See also
Basic Example
const http = const nodeRes = http
API
Response
A simple IO module to make consistent HTTP response, without worrying about underlying details.
- Response
- ~getHeader(res, key) ⇒
Array
|String
- ~header(res, key, value) ⇒
void
- ~append(res, key, value) ⇒
void
- ~status(res, code) ⇒
void
- ~safeHeader(res, key, value) ⇒
void
- ~removeHeader(res, key) ⇒
void
- ~write(res, body) ⇒
void
- ~end(res, [payload]) ⇒
void
- ~send(req, res, body, [generateEtag]) ⇒
void
- ~etag(res, body) ⇒
void
- ~prepare(res, body) ⇒
String
- ~prepareJsonp(res, body, callbackFn) ⇒
String
- ~json(req, res, body, [generateEtag]) ⇒
void
- ~jsonp(req, res, body, [callbackFn], [generateEtag]) ⇒
void
- ~location(res, url) ⇒
void
- ~redirect(req, res, url, [status]) ⇒
void
- ~vary(res, field) ⇒
void
- ~type(req, res, [charset]) ⇒
void
- ~stream(res, body) ⇒
Promise
- ~getHeader(res, key) ⇒
Array
| String
Response~getHeader(res, key) ⇒ Returns the value of an existing header on the response object
Kind: inner method of Response
Returns: Array
| String
- Return type depends upon the header existing value
Param | Type |
---|---|
res | ServerResponse |
key | String |
Example
nodeRes
void
Response~header(res, key, value) ⇒ 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
Param | Type |
---|---|
res | http.ServerResponse |
key | String |
value | String | Array |
Example
nodeRes // or set an array of headersnodeRes
void
Response~append(res, key, value) ⇒ Appends value to the header existing values.
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
key | String |
value | String | Array |
Example
nodeRes // or append an array of headersnodeRes
void
Response~status(res, code) ⇒ Set status on the HTTP res object
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
code | Number |
Example
nodeResstatusres 200
void
Response~safeHeader(res, key, value) ⇒ Sets the header on response object, only if it does not exists.
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
key | String |
value | String | Array |
Example
nodeRes
void
Response~removeHeader(res, key) ⇒ Removes the header from response
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
key | String |
Example
nodeRes
void
Response~write(res, body) ⇒ Write string or buffer to the response object.
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
body | String | Buffer |
Example
nodeRes
void
Response~end(res, [payload]) ⇒ Explictly end HTTP response
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
[payload] | String | Buffer |
Example
nodeRes
void
Response~send(req, res, body, [generateEtag]) ⇒ 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
Param | Type | Default |
---|---|---|
req | http.ServerRequest |
|
res | http.ServerResponse |
|
body | String | Buffer | Object | Stream |
|
[generateEtag] | Boolean |
true |
Example
nodeRes // or htmlnodeRes // or JSONnodeRes // or BuffernodeRes // Ignore etagnodeRes
void
Response~etag(res, body) ⇒ Sets the Etag header for a given body chunk
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
body | String | Buffer |
Example
nodeRes
String
Response~prepare(res, body) ⇒ 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
Param | Type |
---|---|
res | http.ServerResponse |
body | Mixed |
Example
const chunk = nodeRes if chunk nodeRes if nodeReq chunk = null nodeResstatus304 nodeRes
String
Response~prepareJsonp(res, body, callbackFn) ⇒ Prepares response for JSONP
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
body | Object |
callbackFn | String |
Example
const chunk = nodeRes if chunk nodeRes if nodeReq chunk = null nodeResstatus304 nodeRes
void
Response~json(req, res, body, [generateEtag]) ⇒ Returns the HTTP response with Content-type
set to application/json
.
Kind: inner method of Response
Param | Type | Default |
---|---|---|
req | http.IncomingMessage |
|
res | http.ServerResponse |
|
body | Object |
|
[generateEtag] | Boolean |
true |
Example
nodeResnodeRes
void
Response~jsonp(req, res, body, [callbackFn], [generateEtag]) ⇒ Make JSONP response with Content-type
set to
text/javascript
.
Kind: inner method of Response
Param | Type | Default |
---|---|---|
req | http.IncomingMessage |
|
res | http.ServerResponse |
|
body | Object |
|
[callbackFn] | String |
'callback' |
[generateEtag] | Boolean |
true |
Example
nodeRes
void
Response~location(res, url) ⇒ Set Location
header on the HTTP response.
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
url | String |
void
Response~redirect(req, res, url, [status]) ⇒ Redirect the HTTP request to the given url.
Kind: inner method of Response
Param | Type | Default |
---|---|---|
req | http.IncomingMessage |
|
res | http.ServerResponse |
|
url | String |
|
[status] | Number |
302 |
Example
nodeRes
void
Response~vary(res, field) ⇒ Add vary header to the HTTP response.
Kind: inner method of Response
Param | Type |
---|---|
res | http.ServerResponse |
field | String |
void
Response~type(req, res, [charset]) ⇒ 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
Param | Type |
---|---|
req | http.IncomingMessage |
res | http.ServerResponse |
[charset] | String |
Example
nodeRestyperes 'html' nodeRestyperes 'json' nodeRestyperes 'text/html' 'ascii'
Promise
Response~stream(res, body) ⇒ 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
Param | Type |
---|---|
res | Object |
body | Stream |
Example
Response // handle stream errorsResponse