Javascript 如何在快递路由器中使用正文解析器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39985486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use body-parser in express router?
提问by hussain
I have a post api that has object but i am not able to print in console its throowing undefined i thought i am missing body-parser but after adding body parser i see error body-parser deprecated bodyParser: use individual json/urlencoded middlewares
Any help will be appreciated.
我有一个包含对象的 post api,但我无法在控制台中打印它的抛出未定义我以为我缺少正文解析器但是在添加正文解析器后我看到错误body-parser deprecated bodyParser: use individual json/urlencoded middlewares
任何帮助将不胜感激。
routes.js
路由.js
var express = require('express');
var bodyParser = require('body-parser');
var Diagram = require('./api/diagram/diagram.controller');
var router = express.Router();
router.post('/saveUpdateDiagram',bodyParser,function(req,res){
console.log(req.body);
});
app.js
应用程序.js
var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
var router = express.Router();
var route = require('./server/routes').router;
var mongoose = require('mongoose');
mongoose.connection.on('connected', function() {
console.log('MongoDB connected ');
});
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', route);
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
app.listen(8760, function() {
console.log('I am listening 8760...');
});
回答by WindUpDurb
Your use of body-parser in app.js is fine. It is middleware, and it is loaded with app.use so that it will be applied to every incoming request.
您在 app.js 中使用 body-parser 很好。它是中间件,它被 app.use 加载,因此它将应用于每个传入的请求。
You can remove it in routes.js, so that it looks like so:`
您可以在 routes.js 中删除它,使其看起来像这样:`
var express = require('express');
var Diagram = require('./api/diagram/diagram.controller');
var router = express.Router();
router.post('/saveUpdateDiagram', function(req,res){
console.log(req.body);
});
` Also, try replacing:
` 另外,尝试替换:
app.use(bodyParser.urlencoded({
extended: false
}))
with:
和:
app.use(bodyParser.urlencoded({extended: true}));
回答by Dani
It means that using the bodyParser()
constructor has been deprecated, as of 2014-06-19.
这意味着bodyParser()
从 2014 年 6 月 19 日起,不推荐使用构造函数。
app.use(bodyParser()); //Now deprecated You now need to call the methods separately
app.use(bodyParser.urlencoded());
app.use(bodyParser.json()); //And so on.
If you're still getting a warning with urlencoded you need to use
如果您仍然收到 urlencoded 警告,则需要使用
app.use(bodyParser.urlencoded({
extended: true
}))
The extended config object key now needs to be explicitly passed, since it now has no default value, as said over here.
扩展的配置对象键现在需要显式传递,因为它现在没有默认值,如此处所述。
回答by Prajith P
Your use of body-parser in app.js is fine. It is middleware, and it is loaded with app.use but the problem is the position where you used the app.use('/', route); it should be placed belowthe app.use(bodyParser.urlencoded({ extended: true }));
您在 app.js 中使用 body-parser 很好。它是中间件,它加载了 app.use 但问题是你使用 app.use('/', route); 的位置。它应该放在app.use(bodyParser.urlencoded({ extended: true })); 下面。