Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Express BodyParser for file uploads (Node.js)

This seems like it should be a fairly simple question, but I'm having a really hard time figuring out how to approach it.

I'm using Node.js + Express to build a web application, and I find the connect BodyParser that express exposes to be very useful in most cases. However, I would like to have more granular access to multipart form-data POSTS as they come - I need to pipe the input stream to another server, and want to avoid downloading the whole file first.

Because I'm using the Express BodyParser, however, all file uploads are parsed automatically and uploaded and available using "request.files" before they ever get to any of my functions.

Is there a way for me to disable the BodyParser for multipart formdata posts without disabling it for everything else?

like image 402
Myk Avatar asked Jul 02 '12 14:07

Myk


People also ask

Do I need to use bodyParser in Express?

bodyParser was added back to Express in release 4.16. 0, because people wanted it bundled with Express like before. That means you don't have to use bodyParser.

What is the alternative of bodyParser?

node. js - Non-deprecated alternative to body-parser in Express.

Is bodyParser deprecated?

'bodyParser' is deprecated. // If you are using Express 4.16+ you don't have to import body-parser anymore.

What does bodyParser do in node JS?

Body-parser is the Node. js body parsing middleware. It is responsible for parsing the incoming request bodies in a middleware before you handle it.


2 Answers

If you need to use the functionality provided by express.bodyParser but you want to disable it for multipart/form-data, the trick is to not use express.bodyParser directly. express.bodyParser is a convenience method that wraps three other methods: express.json, express.urlencoded, and express.multipart.

So instead of saying

app.use(express.bodyParser()) 

you just need to say

app.use(express.json())    .use(express.urlencoded()) 

This gives you all the benefits of the bodyparser for most data while allowing you to handle formdata uploads independently.

Edit: json and urlencoded are now no longer bundled with Express. They are provided by the separate body-parser module and you now use them as follows:

bodyParser = require("body-parser") app.use(bodyParser.json())    .use(bodyParser.urlencoded()) 
like image 55
Myk Avatar answered Oct 11 '22 15:10

Myk


If the need for body parsing depends only on the route itself, the simplest thing is to use bodyParser as a route middleware function on only the routes that need it rather than using it app-wide:

var express=require('express'); var app=express.createServer(); app.post('/body', express.bodyParser(), function(req, res) {     res.send(typeof(req.body), {'Content-Type': 'text/plain'}); }); app.post('/nobody', function(req, res) {     res.send(typeof(req.body), {'Content-Type': 'text/plain'}); }); app.listen(2484); 
like image 26
ebohlman Avatar answered Oct 11 '22 15:10

ebohlman