异常代码:
\Workspaces\WebStormProject\imooc-project\imooc>node app.js
Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (L:\Workspaces\WebStormProject\imooc-project\imooc\node_modules\express\lib\express.js:89:13)
at Object.<anonymous> (L:\Workspaces\WebStormProject\imooc-project\imooc\app.js:12:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
当前express版本:3.5.3
根据错误可以知道bodyparser已经不和 Express绑定在一起了,说明当前express不包含body-parser,执行如下命令即可:npm install body-parser
解决:
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.use(multer()); // for parsing multipart/form-data
app.post('/', function (req, res) {
console.log(req.body);
res.json(req.body);
})