简体   繁体   中英

req.body is empty when apply middleware to a GET route

I write a router for authentication and a middleware to parse redirect parameter and save in session. But when parsing query parameters, both req.body and req.params are empty.

router.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'));

redirect.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();
  };
};

When I call /auth/facebook?redirect=/groups , the output is:

{}
{}

The code is on Github https://github.com/guoyunhe/shrgrp

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

With GET call you can get data in two ways:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM