Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including stream-body-parser with all npm packages installed. Try it out:

var streamBodyParser = require("stream-body-parser")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

stream-body-parser v0.0.1

Process multipart/form-data on the fly as streams.

#stream-body-parser.js Parse multipart/form-data as streams - processing doing upload


This is a (partial) drop-in for the bodyParser in connect.js and express.js. But instead of just being able to upload files to a directory, it directly access the upload stream, as it being uploaded. Thereby you can redirect the uploaded files to another destination than just the disk - being it a database or simply processing the data as it is being uploaded.

I've created this in order to make video upload service, that automatically transcodes the videos to a specific format on upload. Thereby having the video available in the desired format, already when the user finishes uploading.

So together with stream-transcoder.js, you are able to do stuff like this.

stream-body-parser will default back to connect.js/express.js's bodyParser, when non multipart/form-data data is being posted.

var express = require('express'),
    StreamBodyParser = require('stream-body-parser'),
    Transcoder = require('stream-transcoder');

var app = express();

var bodyParser = new StreamBodyParser(app);

bodyParser.process('video/*', function(stream, req, next) {
    var myGridFSWriteStream = (Some MongoDB GridFS stream)
    new Transcoder(stream)
        .maxSize(320, 240)
        .videoBitrate(800 * 1000)
        .audioBitrate(128 * 1000)
        .on('finish', function() {
});'/', function(req, res) {
    res.send(200); // File uploaded


In the above example the video is transcoded as it is being uploaded, and then piped directly into the database. So when the route is being called, the video is transcoded and stored.

Class StreamBodyParser

This class provides incoming uploaded files as streams, as soon as the request has been made - but before the upload has started.

new StreamBodyParser(app, [options])

  • app Object - express.js or connect.js app.
  • options Object - options.


var bodyParser = require('stream-body-parser');

var bodyParser = new StreamBodyParser({ uploadDir: './public' }, anExpressApp)

Prepares a new StreamBodyParser and - optionally if the app parameter is supplied, configures it for use with connect.js or express.js.

streamBodyParser.process(mime, fn)

  • mime String - Mime type to process with fn. As an example: image/jpeg or video/*. Default value */*
  • fn Function - Processor function for when file of specified mime type is about to be uploaded.
    • fn should be a function accepting three parameters.
      • stream Object - The stream of the file being uploaded.
      • req Object - The req object of connect.js/express.js
      • next Function - Function to be called, when processor is done processing all the data.

If uploadDir is specified by options doing object creation, a processor will be registered to mime type */* that simply pipes the input to a files in the specified directory.

var express = require('express'),
    StreamBodyParser = require('stream-body-parser'),
    Transcoder = require('stream-transcoder');

var app = express();

var bodyParser = new StreamBodyParser(app);

bodyParser.process('image/jpeg', function(stream, req, next) {
    stream.on('end', next);

bodyParser.process(function(stream, req, next) {
    console.log('Stream of type ' + stream.mime + ' is being uploaded.');
    stream.on('end', function() {

The above example simply images of type jpeg to a filename named the same as the incoming file.

The second is processor simply logs that a file is being uploaded, and when it completes.


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free