Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决后端接口的 PayloadTooLargeError: request entity too large 错误 #166

Closed
Dream4ever opened this issue Jul 22, 2021 · 0 comments
Closed
Labels
Back-end Where data really come and go

Comments

@Dream4ever
Copy link
Owner

问题描述

在调用后端接口传输 JSON 数据时,发现后端接口状态为 413,报 PayloadTooLargeError: request entity too large 这个错误。

解决过程

Google 这个报错信息,在 Stack Overflow 上的 Error: request entity too large 这篇讨论中,有人给出了解决办法,像下面这样设置 Express.js 的两个中间件即可:

import { json, urlencoded } from 'body-parser'

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

其中有人说最开始的时候按照上面的代码设置没能解决,于是在 node_modules 中查看并修改源代码才解决。

虽然自己这边按照上面的代码设置就解决了,但出于好奇,还是看了看源代码。最开始找的是上面链接中给出的中间件源码所在路径:node_modules/express/node_modules/connect/lib/middleware/json.js,结果发现 node_modules/express 目录下没有 node_modules 子文件夹,猜测可能是自己用的 Express.js 版本比较新,所以项目结构发生了变化。

后来在根目录的 node_modules 文件夹下找到了 body-parser 这个中间件的文件夹,又在其中找到了 lib\types\json.js 这个中间件的源码,发现其 json 方法中的 limit 变量有 100kb 这么一个默认值,而接口报错时,所传输的数据为 143kb,看来这个中间件就是把传输的 JSON 体积上限默认设置为了 100kb

@Dream4ever Dream4ever added the Back-end Where data really come and go label Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Back-end Where data really come and go
Projects
None yet
Development

No branches or pull requests

1 participant