Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    request-bufferpublic

    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

    Keywords

    none

    install

    npm i request-buffer

    Downloadsweekly downloads

    7

    version

    1.0.3

    license

    none

    repository

    github.com

    last publish

    collaborators

    • avatar