Error: request entity too large

后端 未结 21 3104
无人共我
无人共我 2020-11-22 06:36

I\'m receiving the following error with express:

Error: request entity too large
    at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node         


        
相关标签:
21条回答
  • 2020-11-22 07:10

    I had the same error recently, and all the solutions I've found did not work.

    After some digging, I found that setting app.use(express.bodyParser({limit: '50mb'})); did set the limit correctly.

    When adding a console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/lib/middleware/json.js:46 and restarting node, I get this output in the console:

    Limit file size: 1048576
    connect.multipart() will be removed in connect 3.0
    visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
    connect.limit() will be removed in connect 3.0
    Limit file size: 52428800
    Express server listening on port 3002
    

    We can see that at first, when loading the connect module, the limit is set to 1mb (1048576 bytes). Then when I set the limit, the console.log is called again and this time the limit is 52428800 (50mb). However, I still get a 413 Request entity too large.

    Then I added console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/node_modules/raw-body/index.js:10 and saw another line in the console when calling the route with a big request (before the error output) :

    Limit file size: 1048576
    

    This means that somehow, somewhere, connect resets the limit parameter and ignores what we specified. I tried specifying the bodyParser parameters in the route definition individually, but no luck either.

    While I did not find any proper way to set it permanently, you can "patch" it in the module directly. If you are using Express 3.4.4, add this at line 46 of node_modules/express/node_modules/connect/lib/middleware/json.js :

    limit = 52428800; // for 50mb, this corresponds to the size in bytes
    

    The line number might differ if you don't run the same version of Express. Please note that this is bad practice and it will be overwritten if you update your module.

    So this temporary solution works for now, but as soon as a solution is found (or the module fixed, in case it's a module problem) you should update your code accordingly.

    I have opened an issue on their GitHub about this problem.

    [edit - found the solution]

    After some research and testing, I found that when debugging, I added app.use(express.bodyParser({limit: '50mb'}));, but after app.use(express.json());. Express would then set the global limit to 1mb because the first parser he encountered when running the script was express.json(). Moving bodyParser above it did the trick.

    That said, the bodyParser() method will be deprecated in Connect 3.0 and should not be used. Instead, you should declare your parsers explicitly, like so :

    app.use(express.json({limit: '50mb'}));
    app.use(express.urlencoded({limit: '50mb'}));
    

    In case you need multipart (for file uploads) see this post.

    [second edit]

    Note that in Express 4, instead of express.json() and express.urlencoded(), you must require the body-parser module and use its json() and urlencoded() methods, like so:

    var bodyParser = require('body-parser');
    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
    

    If the extended option is not explicitly defined for bodyParser.urlencoded(), it will throw a warning (body-parser deprecated undefined extended: provide extended option). This is because this option will be required in the next version and will not be optional anymore. For more info on the extended option, you can refer to the readme of body-parser.

    [third edit]

    It seems that in Express v4.16.0 onwards, we can go back to the initial way of doing this (thanks to @GBMan for the tip):

    app.use(express.json({limit: '50mb'}));
    app.use(express.urlencoded({limit: '50mb'}));
    
    0 讨论(0)
  • 2020-11-22 07:10

    In my case it was not enough to add these lines :

    var bodyParser = require('body-parser');
    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
    

    I tried adding the parameterLimit option on urlencoded function as the documentation says and error no longer appears.

    The parameterLimit option controls the maximum number of parameters that are allowed in the URL-encoded data. If a request contains more parameters than this value, a 413 will be returned to the client. Defaults to 1000.

    Try with this code:

    var bodyParser = require('body-parser');
    app.use(bodyParser.json({limit: "50mb"}));
    app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
    
    0 讨论(0)
  • 2020-11-22 07:10

    A slightly different approach - the payload is too BIG

    All the helpful answers so far deal with increasing the payload limit. But it might also be the case that the payload is indeed too big but for no good reason. If there's no valid reason for it to be, consider looking into why it's so bloated in the first place.

    Our own experience

    For example, in our case, an Angular app was greedily sending an entire object in the payload. When one bloated and redundant property was removed, the payload size was reduced by a factor of a 100. This significantly improved performance and resolved the 413 error.

    0 讨论(0)
  • 2020-11-22 07:13

    The following worked for me... Just use

    app.use(bodyParser({limit: '50mb'}));
    

    that's it.

    Tried all above and none worked. Found that even though we use like the following,

    app.use(bodyParser());
    app.use(bodyParser({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb'}));
    

    only the 1st app.use(bodyParser()); one gets defined and the latter two lines were ignored.

    Refer: https://github.com/expressjs/body-parser/issues/176 >> see 'dougwilson commented on Jun 17, 2016'

    0 讨论(0)
  • 2020-11-22 07:14

    For me the main trick is

    app.use(bodyParser.json({
      limit: '20mb'
    }));
    
    app.use(bodyParser.urlencoded({
      limit: '20mb',
      parameterLimit: 100000,
      extended: true 
    }));
    

    bodyParse.json first bodyParse.urlencoded second

    0 讨论(0)
  • 2020-11-22 07:15

    I've used another practice for this problem with multer dependancie.

    Example:

    multer = require('multer');
    
    var uploading = multer({
      limits: {fileSize: 1000000, files:1},
    });
    
    exports.uploadpictureone = function(req, res) {
      cloudinary.uploader.upload(req.body.url, function(result) {
        res.send(result);
      });
    };
    
    module.exports = function(app) {
        app.route('/api/upload', uploading).all(uploadPolicy.isAllowed)
            .post(upload.uploadpictureone);
    };
    
    0 讨论(0)
提交回复
热议问题