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

后端 未结 7 1881
深忆病人
深忆病人 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 18:54

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

提交回复
热议问题