我收到以下错误与快递:

Error: request entity too large
    at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:16:15)
    at json (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/json.js:60:5)
    at Object.bodyParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.cookieParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.logger (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/logger.js:158:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.staticMiddleware [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
TypeError: /Users/michaeljames/Documents/Projects/Proj/mean/app/views/includes/foot.jade:31
    29| script(type="text/javascript", src="/js/socketio/connect.js")
    30| 
  > 31| if (req.host='localhost')
    32|     //Livereload script rendered 
    33|     script(type='text/javascript', src='http://localhost:35729/livereload.js')  
    34| 

Cannot set property 'host' of undefined
    at eval (eval at <anonymous> (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:152:8), <anonymous>:273:15)
    at /Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:153:35
    at Object.exports.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:197:10)
    at Object.exports.renderFile (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:233:18)
    at View.exports.renderFile [as engine] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:218:21)
    at View.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/response.js:801:7)
    at Object.handle (/Users/michaeljames/Documents/Projects/Proj/mean/config/express.js:82:29)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:188:17)

POST /api/0.1/people 500 618ms

我用的是meanstack。我有以下使用语句在我的express.js

//Set Request Size Limit
app.use(express.limit(100000000));

在fiddler中,我可以看到内容长度头的值为:1078702

我想这是八位字节,这是1.0787兆字节。

我不知道为什么express不让我发布json数组,我之前在另一个express项目中发布,该项目没有使用平均堆栈项目结构。


当前回答

我正在使用multer上传文件到AWS s3。 对于我来说,在添加client_max_body_size 100M之后;导入nginx文件, 误差是400。(但是413错误消失了,这意味着它成功地通过nginx到达了你的服务器)

解决方案如下: https://stackoverflow.com/a/71240419/15477963

我的app.js文件不需要改变,保持如下所示:

const express = require('express');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

其他回答

2016年,以上都不适合我,直到我显式设置'类型'除了'限制'身体解析器,示例:

  var app = express();
  var jsonParser       = bodyParser.json({limit:1024*1024*20, type:'application/json'});
  var urlencodedParser = bodyParser.urlencoded({ extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoded' })

  app.use(jsonParser);
  app.use(urlencodedParser);

在我的例子中,添加这些行是不够的:

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

我尝试在urlencoded函数上添加parameterLimit选项,正如文档所述,错误不再出现。

parameterLimit选项控制参数的最大数量 在url编码的数据中是允许的。如果请求包含更多 参数大于此值时,将返回一个413给客户端。 默认值为1000。

试试下面的代码:

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

我最近遇到了同样的问题,咆哮的解决方案对我来说很有效。

依赖: 快递>>版本: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}));

为了理解: HTTP 431

HTTP 413负载过大响应状态码表示 请求实体大于服务器定义的限制;服务器 可能会关闭连接或返回一个Retry-After报头字段。

我也有同样的问题

使用快捷4.+ 我的代码看起来像这样,经过两天的广泛测试,它工作得很好。

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);
        });
    });
};

在我的例子中,问题出在Nginx配置上。为了解决这个问题,我必须编辑文件:/etc/nginx/nginx.conf,并在服务器块中添加这一行:

client_max_body_size 5M;

重新启动Nginx,问题就消失了

sudo systemctl restart nginx