Quick Example
response.json({result:'error',missing_keys:['email']}).status(400).pipe(res)
// headers are { 'content-type': 'application/json',
// date: 'Mon, 12 May 2014 12:57:31 GMT',
// connection: 'keep-alive',
// 'transfer-encoding': 'chunked' }
// statusCode is 400
// body is { result: 'error', missing_keys: [ 'email' ] }
Response
The basic idea is to build request for HTTP Responses.
This whole package is still beta.
files
var server = http
When pipeing files to response
it will lookup the mime type and set the propert content-type header for whatever file extension you send it.
html, json, txt
var server = http
.error(err[, statusCode])
r
r
r
In addition, errors emitted on the stream piped to response
will be passed through the same API and are accesssible in views
.
gzip and deflate compression
The compress
and gzip
keys in an options object are used for compression.
var server = http
You can pass an HTTP Request object and the best compression, if any, will be chosen for you. Alternatively you can pass "gzip"
or "deflate"
to forcce compression of the response stream.
This compression option is compatible with every other feature in response
and will work whether you do file streaming, html, json, or even using views. When passing a view, string or buffer to response
the second argument is used as the options object.
var server = http
status codes and headers
response
also has an extended version of node core's HTTP Response API.
All headers setting and checking is done caseless while preserving the original casing when first set. This way you never accidentally send two of the same header but can still support broken clients that check for specific caseing.
.statusCode
Set the statusCode property to send the HTTP status code. This is a non-destructive way to send the status code.
var r = rstatusCode = 500r
.setHeader(key, value[, clobber=true])
Defaults to clobbering (overwritting) existing values but when disabled will concatenate values.
r
.setHeader(headers)
Set multiple headers by passing an object.
r
.getHeader(key)
You can retreive a header by its key, use this method instead of directly accessing the headers object to avoid caseing constraints.
r
.hasHeader(key)
Check if a header is already set. If one is set the header key will be returned (which is important because it may have different caseing).
r
views (very experimental)
{ if e return } var server = http
This is how you would easily support something like a template system. TODO: example.
Credits
Mad props to @marak who handed over the "response" package in npm that he registered way back in the day.