我在使用护照进行身份验证时,在创建和管理用户时传递了许多不同的对象和设置 . 目前,我的用户创建数据可能看起来像这样(req.body):

bgcolor: '#FFF',
color: '#000',
culture: 'FI_fi',
email: 'asdf',
name: 'asdf',
'sites[-JR_Ef7nfvTdX1d0uNm7]': '-JR_Ef7nfvTdX1d0uNm7',
expire: '1',
active: 'true',
'ac[createUsers]': 'false',
'ac[modifyShifts]': 'false',
'ac[editUsers]': 'false',
'ac[delUsers]': 'false',
'views[defaultView][name]': 'defaultView',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][location]': '0',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][id]': '-JR_Ef7nfvTdX1d0uNm7',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][undefined][location]': '13',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][location]': '14',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][id]': '-JR_EzF6eZWxGic3DN8U',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][users][0][location]': '0',
'views[defaultView][sites][-JR_Ef7nfvTdX1d0uNm7][groups][-JR_EzF6eZWxGic3DN8U][users][0][id]': '0'

基本上,对象数据是字符串而不是json格式 . 我在主app.js中设置了bodyParsers:

passportConfigs(passport);

app.use(cookieParser());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(bodyParser.json());

app.use(session({
    secret: '...' ,
    resave: false,
    saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());

并且数据在atm处理 . 在路由器:

app.post('/add_user', isLoggedIn, function(req, res, next) {
    ... (req.body) is handled here.
}

function isLoggedIn(req, res, next) {

    if (req.isAuthenticated())
        return next();

    res.status(401).send("user not logged in");
    return false;
}

问题是没有bodyParser.urlencoded,护照似乎无法正常工作,它会因“缺少凭证”而出错 .

如何获得使用bodyParser.json()(最佳解决方案)的护照或者将urlencoded版本解析为JSON很好?