Node.js: how to limit the HTTP request size and upload file size?

后端 未结 7 1632
轮回少年
轮回少年 2020-12-08 20:35

I\'m using Node.js and express.

I would like to limit the HTTP request size. Let\'s say, if someone sends me a HTTP request more than 2 MB then I stop the request r

相关标签:
7条回答
  • 2020-12-08 21:20

    Express uses Connect, which has a limit middleware available. You can use this in your Express app by doing something like this:

    app.use(express.limit('2mb'));
    

    That, for example, would limit all HTTP requests to 2 MB. Since uploaded files are part of the HTTP request, any file upload larger than 2 MB would be aborted as well.


    NOTE: This middleware is deprecated and will soon be removed. Discussion on why this is the case is available at: https://github.com/senchalabs/connect/pull/925#issuecomment-26990726

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