2.0.1 • Published 6 years ago

delayed-response v2.0.1

Weekly downloads
29
License
MIT
Repository
github
Last release
6 years ago

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

2.0.1

6 years ago

2.0.0

8 years ago

1.0.0

8 years ago