body-parser 模块处理
请求体中没有数据?
在 express 框架中是这样说的: Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.
意思就是:正常情况下,请求体中的键值对内容默认是未定义的,且当你使用body-parser或者multer中间件时自动填充。
以下是express 官网上的实例 , 注意!新版本multer的引用方式已经改变
var app = require('express')();
var bodyParser = require('body-parser'); var multer = require('multer'); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.post('/', function (req, res) { console.log(req.body); res.json(req.body); // for parsing application/json })
multer 模块处理表单数据
var multer = require('multer');
var storage = multer.memoryStorage();//这样得到buffer数据 var upload = multer({ storage: storage });// for parsing multipart/form-data app.post('/', upload.array('file'), function (req, res) { console.log(req.files); console.log(req.body); res.json(req.body);// for parsing application/json })
err:错误信息 Request Entity Too Large
上传数据大于:{limit :'100kb'}
app.use(bodyParser.urlencoded({ extended: true, limit:'2mb'}))设置2mb数据限制
app.use(bodyParser.json({limit:'1mb'}))设置json大小1mb