简体   繁体   中英

Disable bodyparser for file uploads - Nodejs

This question is very similar to How to disable Express BodyParser for file uploads (Node.js) . The answer they have provided is for Express3 and I have tried the solution with the updated Express 4 and it does not seem to work.

I'm using Node.js + Express to build a web application. I am using another library,BodyParser,to parse post parameters. 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.

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?

This is my app.js file. In here I am defining an authentication route which shouldn't except any files just a token (POST parameter). I am also defining another route called upload. This route accepts a file and also POST parametes (form-data). This route only gets called if the authentication route allows it. So in the authetnication route I don't want form-data to be allowed, but in the upload route I do. So when I get a request to uplaod something it will go through the auth route and then the upload route. Due to this I need to allow the auth route to allow files (form-data) which I do not want. So I want bodyparser to work in the auth route while I use mutler (another library) in my upload path to parse my upload files. In my real application though of course I have many more routes and would like to code it as cleanly as I can with the least amount of redundancy.

var express = require('express');
var app = express();


var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
    extended: true
}));


var route_auth = require('./routes/auth');
app.use('/api/post/*', route_auth);
var route_upload = require('./routes/post/upload');
app.use('/api/post/upload', route_upload );


app.listen(3000, function() {
    console.log('Server listening on port 3000!')
});

My auth route looks something like this:

router.post("/", function(req, res, next) {
       if(everythingiscool){
            return next()
        }
           next(err);
});

My upload route looks like this:

var express = require('express');
var router = express.Router();
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' });

router.post("/", upload.single('avatar'), function(req, res, next) {
         //work with req.file
});

Wrap the bodyParse middleware in a function that checks if the request body's Content-Type is multipart or not:

var isMultipart = /^multipart\//i;
var bodyParser = require('body-parser');
var urlencodedMiddleware = bodyParser.urlencoded({ extended: true });
app.use(function (req, res, next) {
  var type = req.get('Content-Type');
  if (isMultipart.test(type)) return next();
  return urlencodedMiddleware(req, res, next);
});

Instead of disabling, why not enable the middleware on the routes/routers where you need it?

For individual routes, you can just add it as another argument before your actual route handler, for example:

app.post('/upload', bodyParser, (req, res) => {
  // route logic here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM