gpt4 book ai didi

javascript - 错误 : request entity too large

转载 作者:IT老高 更新时间:2023-10-28 11:02:20 26 4
gpt4 key购买 nike

我收到以下 express 错误:

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 中,我可以看到内容长度 header ,其值为:1078702

我相信这是八位字节,这是 1.0787 兆字节。

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

最佳答案

我最近遇到了同样的错误,我找到的所有解决方案都不起作用。

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

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

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 Request entity too large .

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

Limit file size: 1048576

这意味着不知何故,某处,connect重置限制参数并忽略我们指定的内容。我尝试指定 bodyParser单独路由定义中的参数,但也没有运气。

虽然我没有找到任何适当的方法来永久设置它,但您可以直接在模块中“patch”它。如果您使用的是 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 版本不同,行号可能会有所不同。请注意,这是不好的做法,如果您更新模块,它将被覆盖

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

我打开了an issue on their 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'}));

如果您需要分段(用于文件上传),请参阅 this post .

[第二次编辑]

请注意,在 Express 4 中,而不是 express.json()express.urlencoded() ,您必须要求 body-parser模块并使用它的json()urlencoded()方法,像这样:

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

如果 extended选项未明确定义 bodyParser.urlencoded() ,它会抛出一个警告( body-parser deprecated undefined extended: provide extended option )。这是因为在下一版本中将需要此选项,并且将不再成为 optional 。欲了解更多信息,请访问 extended选项,可以引用readmebody-parser .

[第三次编辑]

似乎在 Express v4.16.0 之后,我们可以回到最初的做法(感谢@GBMan 的提示):

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

关于javascript - 错误 : request entity too large,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com