Module that buffers incoming HTTP requests' data, allowing parallel treatments
When building an HTTP-based application, our lovely node.js needs us to define as soon as possible callback for "data" and "end" events, refering to, respectively, the received HTTP request's body's chunk, and the end of this body.
However, this can lead to some issue. The fact is, like in almost all event-programming langages, if you don't define a gesture to a given event, then, when this event is fired, it will be ignored by your application. Thus, if you process some operations like authentication or whatever on your headers before defining your callbacks to "data" and "end" events, the data (that is the received HTTP request's body) will be received by your application, but ignored, as no callback are defined. Annoying isn't it ?
On the other hand, it's pretty ugly and borring to, first, receive your HTTP body, and then, go through all your functions with this data passed as argument. Moreover, your code will spend time to received the data first, and then, continue for your pre-operations. Also, it's stupid to receive a data, and then to authenticate an user and finally realize that this user doesn't have access to your services right ? Well.
This module adds an object to the http.ServerRequest object received through the 'request' callback. This particular object, "streamBuffer", will stream the incoming HTTP body "in background", letting you process all your pre-operations.
var HttpStreamBuffer = require"httpstreambuffer";var http = require"http";var server = httpcreateServer// Adds the streamBuffer object to reqreq;// You can process all your pre-operation here// The incoming HTTP data are received through the streamBuffer obj// ...// This will keep buffering data if not done yet// or will call directly the callback as many times as the number of// chunks it has received// Total transparency !reqstreamBufferonDataconsole.log"Receiving data: " + chunk;;// Same as the method abovereqstreamBufferonEndconsole.log"Request ended.";;;serverlisten8080console.log"Listening on port 8080";;
Feel free to clone this repo, or install it through the well-known npm
npm install httpstreambuffer
This repo and the package uploaded on NPM are normally always both up-to-date