Express(或Connect)的bodyParser中间件被标记为已弃用,建议用户使用:
app.use(connect.urlencoded()) app.use(connect.json())
但是,当我在Action中运行Node.js中的一个示例时,我使用curl按照本书的建议填写表单:
curl -F entry[title]='Ho ho ho' -F entry[body]='santa loves you' http://abc:123@127.0.0.1:3000/api/entry
它不起作用.req.body
没有定义.我错过了什么吗?它与bodyParser一起工作正常.
编辑:快递4的解决方案
以这种方式解析json:
var bodyParser = require('body-parser'); ... app.use(bodyParser.json());
以这种方式解析urlencoded的身体:
app.use(bodyParser.urlencoded({extended: true}));
然后没有弃用警告.extended:true(默认值)使用qs模块,false使用querystring模块解析主体.
不要使用app.use(bodyParser())
,现在不推荐使用该用法.