Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
HTTP Request Buffer for Node.js
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
src
test
.gitignore
.travis.yml
LICENSE
README.md
package.json

README.md

Node RequestBuffer

An HTTP Request Buffer for Node.js

Build Status

RequestBuffer enables late binding to the data and end events for HTTP requests.

The Problem

Binding to the standard data and end events in Node.js' http.ServerRequest can get complicated once your application needs to perform an async call (such as authenticating a user) before processing an HTTP request body.

// ...
authenticateUser(function(authed) {
  if(authed) {
    request.on('data', function(chunk) {
      // It may be too late to bind at this point
    });
  }
});

The Solution

The simple solution is to always make sure you bind to the request events before any asynchronous functions are called. But passing the request data around and handling it properly can be difficult, especially when using a framework where the request data may need to be passed around by several methods.

RequestBuffer enables you to bind to the data event whenever you need without missing any data.

Example

var requestBuffer = require('request-buffer');
// ...
// Add the request buffer to the request object before any async calls
server.request(function (req, res){
  // Add the buffer to the req object
  req.buffer = new requestBuffer(req);
});
// ...
authenticateUser(function(authed) {
  // Once you're done with async calls, attach to the buffer
  req.buffer.attach();

  if(authed) {
    // Bind to the 'data' and 'end' events inside req.buffer
    req.buffer.on('data', function(chunk) {
      // All your data will be emitted no matter where in the request we've attached
    });
  }
});

Installation

npm install request-buffer

Something went wrong with that request. Please try again.