本文介绍了正文解析器在快速路由器中记录空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,我可以在路线上的快递服务器中看到我的req.body
for some reason I can see my req.body in my express server on my route
req body is [Object: null prototype] { '{"password":"xxxxxxxx"}': '' }
但是当我登录req.body.password(对象密钥)时,我得到了
but when I log req.body.password (the object key) I get
req body is undefined
这是我的快递应用中供参考的索引路由器
here's my index router for reference in my express app
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser')
const path = require('path');
/* GET adminPanel. */
router.post('/authenticate', function(req, res, next) {
console.log('req body is',req.body.password)
res.send("passconfirmed");
});
module.exports = router;
推荐答案
要访问主体的内容,请在处理程序之前在中间件中解析传入的请求主体,该处理程序可在req.body属性下找到.
To access the content of the body, Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
您需要安装 body-parser
软件包.
npm i body-parser-保存
现在在项目中导入 body-parser
.应该在定义的路由功能之前调用它.
Now import body-parser
in your project.It should be called before your defined route functions.
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser')
const path = require('path');
app.use(bodyParser.json());
app.use(bodyparser.urlencoded({ extended : true }));
/* GET adminPanel. */
router.post('/authenticate', function(req, res, next) {
console.log('req body is',req.body.password)
res.send("passconfirmed");
});
module.exports = router;
这篇关于正文解析器在快速路由器中记录空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!