1 回答
TA贡献1841条经验 获得超3个赞
在这个简单的测试中,maxBodyLength 似乎对我有用,我将数据上传到本地 Express 服务器。如果我尝试上传超过 maxBodyLength 的内容,我会收到与您相同的错误。所以我怀疑还有更多的东西,比如在你的情况下发生的重定向触发了错误。
此处记录的 axios 问题似乎引用了该问题,它建议将 maxContentLength 设置为 Infinity(正如其他评论者所建议的那样)。
例如
maxContentLength: Infinity,
maxBodyLength: Infinity
测试代码如下:
const axios = require("axios");
function generateRandomData(size) {
const a = Array.from({length: size}, (v, k) => Math.floor(Math.random()*100));
return { data: a, id: 1 };
}
async function uploadData(url, size) {
let params = generateRandomData(size);
let stringData = JSON.stringify(params);
console.log(`uploadData: Uploading ${stringData.length} byte(s)..`);
let resp = await axios({
method: 'post',
url: url,
data: stringData,
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
maxContentLength: 100000000,
maxBodyLength: 1000000000
}).catch(err => {
throw err;
})
console.log("uploadData: response:", resp.data);
return resp;
}
uploadData("http://localhost:8080/upload", 10000000);
对应的服务器代码:
const express = require("express");
const port = 8080;
const app = express();
const bodyParser = require('body-parser')
app.use(bodyParser.json({limit: '50mb'}));
app.post('/upload', (req, res, next) => {
console.log("/upload: Received data: body length: ", req.headers['content-length']);
res.json( { status: 'ok', bytesReceived: req.headers['content-length']});
})
app.listen(port);
console.log(`Serving at http://localhost:${port}`);
添加回答
举报