关于express4不再支持body-parser

express的bodyParser能将表单里的数据格式化,bodyParser原是绑定在express中的,但从express4开始,不在绑定了

如果依然直接使用app.use(express.bodyParser()),会出现如下错误

出现这个问题是因为你安装的是express4,而bodyParser是绑定在 Express 2 or 3中的。

如果仍想使用,可以npm install express@3。

或者也可以:

1. Install body-parser via 'npm install body-parser'
2. Include module by 'var bodyParser = require('body-parser');'
3. Add 'app.use(bodyParser());'

这次服务run起来了,但会出现警告

这次是因为 using bodyParser() on it's own has been deprecated

也就是说

app.use(bodyParser()); //Now deprecated

你需要这样用

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

OK,但是依然有一个警告



app.use(bodyParser.urlencoded())改成:
app.use(bodyParser.urlencoded({
  extended: true
}));
终于没有警告了。。。

参考:https://github.com/martindale/snarl/issues/51
   

   http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4
 
原文地址:https://www.cnblogs.com/alexandra/p/4099610.html