Express.js中不推荐使用的替代body解析器的方法 [英] Non-deprecated alternative to body-parser in Express.js
本文介绍了Express.js中不推荐使用的替代body解析器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读《使用Node和Express进行Web开发》一书,遇到了麻烦.
I am going through the book Web Development with Node and Express and have hit a snag.
我被指示将以下内容放入我的应用程序文件中,但似乎已弃用了 body-parser
,并且无法正常工作.如何实现相同的功能?
I was instructed to put the below in my application file, but it looks like body-parser
is deprecated and will not work. How can I achieve the same functionality?
这是我当前的代码:
app.use(require('body-parser')());
app.get('/newsletter', function(req, res){
// we will learn about CSRF later...for now, we just
// provide a dummy value
res.render('newsletter', { csrf: 'CSRF token goes here' });
});
app.post('/process', function(req, res){
console.log('Form (from querystring): ' + req.query.form);
console.log('CSRF token (from hidden form field): ' + req.body._csrf);
console.log('Name (from visible form field): ' + req.body.name);
console.log('Email (from visible form field): ' + req.body.email); res.redirect(303, '/thank-you');
});
推荐答案
来自: bodyParser已弃用表示4
这意味着,自2014年6月19日起,不推荐使用bodyParser()构造函数.
It means that using the bodyParser() constructor has been deprecated, as of 2014-06-19.
app.use(bodyParser()); //Now deprecated
您现在需要分别调用方法
You now need to call the methods separately
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
以此类推.
这篇关于Express.js中不推荐使用的替代body解析器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文