body-parser 解析post的表单时出错
发布于 7 年前 作者 spursy 3825 次浏览 来自 问答

我是使用表单的类型是: headers: { ‘Content-Type’: ‘application/json;charset=utf-8’ } 我在表单里面传输的是数据对象,对象的数据是base64 的图片,我的服务端是通过body-parser 解析post的表单,但是在解析数据时总是报错。 当我把post的对象属性值改为远小于base64 位的数据时就可以了。是不是因为post的数据太大了,求大神指教。

下面是server 端的代码:

var express = require('express');
var bodyParser = require('body-parser');
var app = module.exports = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/upload_base64', function (req, res) {
    console.log('uploadfile...'+JSON.stringify(req.body));  // 上传的文件信息
})

错误信息如下

OST /api/upload/upload_base64 413 4.489 ms - 3205
[2017-10-12 16:25:53.021] [ERROR] console - Error
    at readStream (D:\1MyWorkWechat\WeChatPlatform\node_modules\raw-body\index.j
s:196:17)
    at getRawBody (D:\1MyWorkWechat\WeChatPlatform\node_modules\raw-body\index.j
s:106:12)
    at read (D:\1MyWorkWechat\WeChatPlatform\node_modules\body-parser\lib\read.j
s:76:3)
    at jsonParser (D:\1MyWorkWechat\WeChatPlatform\node_modules\body-parser\lib\
types\json.js:121:5)
    at Layer.handle [as handle_request] (D:\1MyWorkWechat\WeChatPlatform\node_mo
dules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\rou
ter\index.js:312:13)
    at D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\index.js:
280:7
    at Function.process_params (D:\1MyWorkWechat\WeChatPlatform\node_modules\exp
ress\lib\router\index.js:330:12)
    at next (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\ind
ex.js:271:10)
    at multerMiddleware (D:\1MyWorkWechat\WeChatPlatform\node_modules\multer\lib
\make-middleware.js:18:41)
    at Layer.handle [as handle_request] (D:\1MyWorkWechat\WeChatPlatform\node_mo
dules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\rou
ter\index.js:312:13)
    at D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\index.js:
280:7
    at Function.process_params (D:\1MyWorkWechat\WeChatPlatform\node_modules\exp
ress\lib\router\index.js:330:12)
    at next (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\ind
ex.js:271:10)
    at expressInit (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\mid
dleware\init.js:33:5)
    at Layer.handle [as handle_request] (D:\1MyWorkWechat\WeChatPlatform\node_mo
dules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\rou
ter\index.js:312:13)
    at D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\index.js:
280:7
    at Function.process_params (D:\1MyWorkWechat\WeChatPlatform\node_modules\exp
ress\lib\router\index.js:330:12)
    at next (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\ind
ex.js:271:10)
    at query (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\middlewar
e\query.js:49:5)
    at Layer.handle [as handle_request] (D:\1MyWorkWechat\WeChatPlatform\node_mo
dules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\rou
ter\index.js:312:13)
    at D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\index.js:
280:7
    at Function.process_params (D:\1MyWorkWechat\WeChatPlatform\node_modules\exp
ress\lib\router\index.js:330:12)
    at next (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib\router\ind
ex.js:271:10)
    at Function.handle (D:\1MyWorkWechat\WeChatPlatform\node_modules\express\lib
\router\index.js:176:3)
2 回复

设置下上传大小试试

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

上传文件的header 应该是Content-Type:multipart/form-data application/json 肯定会限制大小的

回到顶部