gpt4 book ai didi

javascript - 将中间件应用于 GET 路由时,req.body 为空

转载 作者:行者123 更新时间:2023-12-03 04:33:54 25 4
gpt4 key购买 nike

我编写了一个用于身份验证的路由器和一个用于解析重定向参数并保存在 session 中的中间件。但是解析查询参数时,req.body和req.params都为空。

路由器.js

/* Route for Facebook login. This is the only login option for now. */

var router = require('express').Router();
var passport = require('passport');
var redirect = require('../middlewares/redirect');

/**
* facebook oauth
*/
router.get('/facebook', redirect.parse(), passport.authenticate('facebook'));

重定向.js

/**
* save the redirect parameter to session
*
* @param {Object} options options for the middleware
*/
module.exports.parse = function (options) {
return function (req, res, next) {
console.log(req.body);
console.log(req.params);
if (req.body.redirect) {
req.session.redirect = req.body.redirect;
console.log(req.body.redirect);
console.log(req.session.redirect);
req.session.save();
}
next();
};
};

当我调用 /auth/facebook?redirect=/groups 时,输出为:

{}
{}

代码位于Github https://github.com/guoyunhe/shrgrp

最佳答案

You are calling GET and expecting data from req.body which is not gonna work

通过 GET 调用,您可以通过两种方式获取数据:

if it is parameter then you can get in req.params

If it is query string then you can get in req.query

关于javascript - 将中间件应用于 GET 路由时,req.body 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com