-
Notifications
You must be signed in to change notification settings - Fork 6
解决后端接口的 PayloadTooLargeError: request entity too large 错误 #166
Copy link
Copy link
Closed
Labels
Back-endWhere data really come and goWhere data really come and go
Description
问题描述
在调用后端接口传输 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。
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Back-endWhere data really come and goWhere data really come and go