Error: request entity too large

后端 未结 21 3150
无人共我
无人共我 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:02

    The better use you can specify the limit of your file size as it is shown in the given lines:

    app.use(bodyParser.json({limit: '10mb', extended: true}))
    app.use(bodyParser.urlencoded({limit: '10mb', extended: true}))
    

    You can also change the default setting in node-modules body-parser then in the lib folder, there are JSON and text file. Then change limit here. Actually, this condition pass if you don't pass the limit parameter in the given line app.use(bodyParser.json({limit: '10mb', extended: true})).

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

    Little old post but I had the same problem

    Using express 4.+ my code looks like this and it works great after two days of extensive testing.

    var url         = require('url'),
        homePath    = __dirname + '/../',
        apiV1       = require(homePath + 'api/v1/start'),
        bodyParser  = require('body-parser').json({limit:'100mb'});
    
    module.exports = function(app){
        app.get('/', function (req, res) {
            res.render( homePath + 'public/template/index');
        });
    
        app.get('/api/v1/', function (req, res) {
            var query = url.parse(req.url).query;
            if ( !query ) {
                res.redirect('/');
            }
            apiV1( 'GET', query, function (response) {
                res.json(response);
            });
        });
    
        app.get('*', function (req,res) {
            res.redirect('/');
        });
    
        app.post('/api/v1/', bodyParser, function (req, res) {
            if ( !req.body ) {
                res.json({
                    status: 'error',
                    response: 'No data to parse'
                });
            }
            apiV1( 'POST', req.body, function (response) {
                res.json(response);
            });
        });
    };
    
    0 讨论(0)
  • 2020-11-22 07:04

    I don't think this is the express global size limit, but specifically the connect.json middleware limit. This is 100kb by default when you use express.bodyParser() and don't provide a limit option.

    Try:

    app.post('/api/0.1/people', express.bodyParser({limit: '5mb'}), yourHandler);
    
    0 讨论(0)
  • 2020-11-22 07:06

    For express ~4.16.0, express.json with limit works directly

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

    For those who start the NodeJS app in Azure under IIS, do not forget to modify web.config as explained here Azure App Service IIS "maxRequestLength" setting

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

    I faced the same issue recently and bellow solution workes for me.

    Dependency : 
    express >> version : 4.17.1
    body-parser >> version": 1.19.0
    
    const express = require('express');
    const bodyParser = require('body-parser');
    
    const app = express(); 
    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
    

    For understanding : HTTP 431

    The HTTP 413 Payload Too Large response status code indicates that the request entity is larger than limits defined by server; the server might close the connection or return a Retry-After header field.

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