当我使用nodejs
+ express
和body-parser
软件包来管理服务器端的路由和数据时,我正在构建一个小型项目。
在前端,我有一个简单的react
应用,该应用已安装了tinyMCE
编辑器。
问题是,当我选择要插入到文档中的图像时,编辑器将其设置为base64
blob,并且当我尝试保存包括该base64图像(通过对服务器执行PUT请求)的更改时,节点将抛出错误500。
刚开始,我就想这是git问题主题之一建议的应用程序json标头的问题。
所以我切换到了
"Content-Type": "application/x-www-form-urlencoded"
问题仍然存在。
然后我尝试使用一些小图像16x16
(以前是340x300
),并且效果很好...
因此,这可能意味着POST在数据部分中的字符过多,但我认为限制为1.9GB
。
这是一些服务器代码示例:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
router.put("/:id", update);
const update = (req, res, next) => {
documentController.update(req, res, next);
};
const update = async (req, res, next) => {
const { name } = req.body;
const document = await Document.findOne({ name });
...
document
.save()
.then(document => {
...
})
.catch(err => next(err));
};
和前端请求:
request = async (url, method, data) => {
try {
let headers = {
//"Content-Type": "application/json"
"Content-Type": "application/x-www-form-urlencoded"
};
let config = {
headers,
method
};
if (data !== undefined) {
//config["body"] = data;
let query = "";
data = JSON.parse(data);
for (let key in data) {
query +=
encodeURIComponent(key) + "=" + encodeURIComponent(data[key]) + "&";
}
query = query.slice(0, -1);
config["body"] = query;
}
let response = await fetch(url, config).catch(error =>
console.log(error)
);
let json = await response.json();
return json;
} catch (error) {
console.log(error);
}
};
也许有人可以指出图像较大时PUT请求的问题以及解决方法。
编辑
是,因为我怀疑是大字符串问题,所以检查了错误:
消息:“请求实体太大”, 预期:328465, 长度:328465, 限制:102400,
编辑2
这是解决问题的完整解决方案
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000
})
);
app.use(bodyParser.json({ limit: "50mb" }));