delayed-response
v2.0.1
Published
Buffer an HTTP response and execute a function before flushing
Downloads
22
Maintainers
Readme
delayed-response
Buffer an HTTP response and execute a function before flushing
Install
$ npm install --save delayed-response
Usage
var DelayedResponse = require('delayed-response')
server.on('request', function (req, res) {
var delayed = DelayedResponse(req, res, print)
delayed.statusCode = 201
fs.createReadStream('data.txt')
.pipe(delayed)
.on('error', function (err) {
res.statusCode = 500
res.end(err.message)
})
})
function print (delayed, data, callback) {
console.log(data.toString())
callback()
}
API
DelayedResponse(req, res, callback)
-> object
Returns a delayed
instance which proxies an http.ServerResponse. Delayed responses emit errors returned from the supplied callback
. Make sure to handle the 'error'
event on your response or use an abstraction like pump.
req
Required
Type: object
res
Required
Type: object
The original http.ServerResponse
.
callback
Required
Type: function
Arguments: req, delayed, buffer, done
A callback to call with the delayed response, the full data buffer (a Buffer
), and a done
function. Passing an error to done
will cause the delayed request to reset the original response headers and then emit an error. Otherwise, the buffered data is re-sent.
License
MIT © Ben Drucker