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

后端 未结 7 1865
深忆病人
深忆病人 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:19

    With express v4, and body-parser v1.17 and above,
    You can pass a function in the type of bodyParser.json.
    body-parser will parse only those inputs where this function returns a truthy value.

    app.use(bodyParser.json({
        type: function(req) {
            return req.get('content-type').indexOf('multipart/form-data') !== 0;
        },
    }));
    

    In the above code,
    the function returns a falsy value if the content-type is multipart/form-data.
    So, it does not parse the data when the content-type is multipart/form-data.

    0 讨论(0)
提交回复
热议问题