An HTTP Request Buffer for Node.js
RequestBuffer enables late binding to the
end events for HTTP requests.
Binding to the standard
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.
// ...authenticateUserifauthedrequeston'data'// It may be too late to bind at this point;;
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.
var requestBuffer = require'request-buffer';// ...// Add the request buffer to the request object before any async callsserverrequest// Add the buffer to the req objectreqbuffer = req;;// ...authenticateUser// Once you're done with async calls, attach to the bufferreqbufferattach;ifauthed// Bind to the 'data' and 'end' events inside req.bufferreqbufferon'data'// All your data will be emitted no matter where in the request we've attached;;
npm install request-buffer