我收到以下错误与快递:

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项目中发布,该项目没有使用平均堆栈项目结构。


当前回答

如果有人尝试了所有的答案,但还没有任何成功,并使用NGINX托管站点,将这一行添加到/etc/nginx/sites-available

client_max_body_size 100M; #100mb

其他回答

更好的使用方法是,你可以指定文件大小的限制,如下所示:

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

您也可以在node-modules body-parser中更改默认设置,然后在lib文件夹中,有JSON和文本文件。然后改变这里的极限。实际上,如果你没有在给定的行中传递limit参数,这个条件就会通过 app.use (bodyParser。Json ({limit: '10mb', extended: true}))。

我最近也犯了同样的错误,我找到的所有解决方案都不管用。

经过一番挖掘,我发现设置app.use(express。bodyParser({限制:50 mb的}));你的限制设置正确了。

当添加一个console.log('Limit file size: '+ Limit);在node_modules/express/node_modules/connect/lib/middleware/json.js:46并重新启动节点,我在控制台中得到以下输出:

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

首先,我们可以看到,在加载connect模块时,限制被设置为1mb(1048576字节)。然后,当我设置限制时,再次调用console.log,这一次限制是52428800 (50mb)。然而,我仍然得到一个413请求实体太大。

然后我添加console.log('限制文件大小:'+ Limit);在node_modules/express/node_modules/connect/node_modules/raw-body/index.js:10中,当使用大请求调用路由时(在错误输出之前),在控制台中看到了另一行:

Limit file size: 1048576

这意味着以某种方式,在某个地方,connect将重置限制参数并忽略我们指定的内容。我尝试在路由定义中单独指定bodyParser参数,但也没有成功。

虽然我没有找到任何合适的方法来永久地设置它,但你可以直接在模块中“修补”它。如果你使用的是Express 3.4.4,在node_modules/ Express /node_modules/connect/lib/middleware/json.js的第46行添加这个:

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

如果您没有运行相同版本的Express,行号可能会不同。 请注意,这是不好的做法,如果你更新你的模块,它将被覆盖。

因此,这个临时解决方案目前有效,但一旦找到解决方案(或模块修复,以防它是模块问题),您就应该相应地更新代码。

我已经在他们的GitHub上开了一个关于这个问题的问题。

[编辑-找到解决方案]

经过一些研究和测试,我发现在调试时,我添加了app.use(express。bodyParser({limit: '50mb'}));,但在app.use(express.json());之后。然后Express将全局限制设置为1mb,因为运行脚本时遇到的第一个解析器是Express .json()。将bodyParser移到上面就可以了。

也就是说,bodyParser()方法将在Connect 3.0中被弃用,不应该被使用。相反,你应该显式地声明你的解析器,就像这样:

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

如果你需要多部分(文件上传),请参阅这篇文章。

(第二个编辑)

注意,在Express 4中,你必须使用body解析器模块并使用它的json()和urlencoded()方法,而不是Express .json()和Express .urlencoded(),如下所示:

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

如果没有为bodyParser.urlencoded()显式定义扩展选项,它将抛出警告(body-parser不支持未定义扩展:提供扩展选项)。这是因为这个选项在下一个版本中是必需的,不再是可选的。有关扩展选项的更多信息,可以参考body-parser的自述文件。

(第三个编辑)

似乎在Express v4.16.0以后,我们可以回到最初的方式来做这件事(感谢@GBMan的提示):

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

此问题在两种情况下发生:

请求体太大,服务器无法处理这么大的数据。这个就可以了

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

2- req.cookies is too large. When testing different next.js applications on the same browser, each time each app was starting on a different port if there were running some apps. Same app might end up starting at port 3000-3005 range. That means if your app saves cookie, that cookie will be saved for each port. Let's say you started 5 different apps at localhost:3000, and each one saved a cookie. If you make a request, all the cookies will be attached to the request object, in this case you will not able to process even small size of post.body. Solution is you have to delete all the cookies

表达4.17.1

app.use( express.urlencoded( {
    extended: true,
    limit: '50mb'
} ) )

演示csb

一个稍微不同的方法——有效载荷太大了

到目前为止,所有有用的答案都涉及增加有效载荷限制。但也可能是载荷确实太大了,但没有好的理由。如果没有合理的理由,那么首先考虑一下为什么它会如此膨胀。

我们自己的经验

例如,在我们的例子中,一个Angular应用贪婪地在有效负载中发送了整个对象。当删除一个臃肿和冗余的属性时,有效负载大小减少了100倍。这极大地提高了性能并解决了413错误。