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

后端 未结 7 1661
轮回少年
轮回少年 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:08

    For an updated solution that isn't deprecated, you can add the limit like so, in your app.js file:

    app.use(express.json({limit: '2mb'}));
    app.use(express.urlencoded({limit: '2mb', extended: false}));
    

    You can also do it like this:

    app.use(express.json({limit: 2000000}));
    app.use(express.urlencoded({limit: 2000000, extended: false}));
    

提交回复
热议问题