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

后端 未结 7 1896
深忆病人
深忆病人 2020-11-30 18:26

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 we

7条回答
  •  甜味超标
    2020-11-30 19:00

    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);
    

提交回复
热议问题