How can I intercept a multipart file stream in Express.js?

Following the example

https://github.com/visionmedia/express/blob/master/examples/multipart/app.js

Express.js seems to do all the work behind the scenes and saves the file and gives it to you whole. How can I intercept and manipulate the raw stream? Specifically I would like to get the stream so I can hook it up to a write stream.

You can't, unless you make a middleware that uses formidable and intercept the stream there (a solution would be to change the bodyParser used by Express with a custom one).

Check out for yourself in the following file: https://github.com/senchalabs/connect/blob/master/lib/middleware/multipart.js

Notice there's no 'global' event that you can hook into.

You can use this:

app.use(express.multipart({ defer: true }));

Then, in the route:

app.post('/upload', function (request, response, next) {
  request.form.onPart = function (part) {
    // Default handling for non-files
    if (!part.filename) return form.handlePart(part);

    // part is a stream of the file
  };

  request.form.parse(request, function (error) {
    if (error) return next(error);
  });
});