简体   繁体   English

Body-Parser太大的Nodejs

[英]Body-Parser too large Nodejs

I am currently developing a API which should receive an image's base64, but when I try to pass this into the payload and send by POST, the Server (which by the way is made with Node, using express and body-parser as middleware) gives me the following error: 我目前正在开发一个应该接收图像的base64的API,但是当我尝试将其传递到有效载荷并通过POST发送时,服务器(顺便说一下,使用Node制作,使用express和body-parser作为中间件)给出我有以下错误:

request entity too large

After searching a lot in the web, I found people saying that I should use: 在网上搜索了很多,我发现有人说我应该使用:

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

intead of using: 使用内容:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

However, by doing so, I receive this error: 但是,通过这样做,我收到此错误:

Unexpected token i in JSON at position 5

Does anyone know what I am missing here? 有谁知道我在这里缺少什么? Thanks in advance! 提前致谢!

This is a print with the object I am trying to send as an example: 这是一个打印与我试图发送的对象作为一个例子:

具有base64属性的对象

Thank you all that helped me out on this issue! 谢谢大家帮助我解决这个问题! For those who are facing or will face this, I was able to solve that by passing an object to the bodyParser.urlencoded method and BodyParser.json method. 对于那些正面临或将面临这种情况的人,我能够通过将对象传递给bodyParser.urlencoded方法和BodyParser.json方法来解决这个问题。

Since they accept an object in their methods, we can pass the limit and the size of the request, So I configure mine doing like that: 由于他们在方法中接受一个对象,我们可以传递请求的限制和大小,所以我配置我这样做:

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({
    limit: '5mb',
    parameterLimit: 100000,
    extended: false 
}));

app.use(bodyParser.json({
    limit: '5mb'
}));

There is this topic here which can help as well: 这里有这个主题也有帮助:

Issue on Stack 堆栈问题

There is also the documentation of BodyParser itself, which you can acess by clicking documentation about BodyParser 还有BodyParser本身的文档,您可以通过单击有关BodyParser的文档来访问

尝试交换bodyParser.jsonbodyParser.urlencoded

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM