delayed-response

2.0.1 • Public • Published

delayed-response Build Status

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

An http.IncomingMessage.

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

Package Sidebar

Install

npm i delayed-response

Weekly Downloads

3

Version

2.0.1

License

MIT

Unpacked Size

7.74 kB

Total Files

5

Last publish

Collaborators

  • bendrucker