Buffer an HTTP response and execute a function before flushing
$ npm install --save delayed-response
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()
}
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.
Required
Type: object
Required
Type: object
The original http.ServerResponse
.
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.
MIT © Ben Drucker