-1

Here in the GET /facebook route i receive the authorization code from front-end after receiving that, i have to make a POST request to /facebook/signin

How can I redirect GET /facebook to POST /facebook/signin

router.get('/facebook', (req, res) => {
    // res.send(req.query.code);
    res.redirect('/facebook/sign'); // = GET /facebook/sign
});

POST route

router.post('/facebook/sign', (req, res, next) => {
    console.log(req.query.code);
    // i will do mine stuff....
});
p0k8h
  • 5,826
  • 4
  • 24
  • 36
  • It is perfectly possible but do you plan on adding a request body? If so, you should read this topic. http://stackoverflow.com/questions/978061/http-get-with-request-body – Nico Van Belle Mar 06 '17 at 07:05
  • Possible duplicate of [Calling already defined routes in other routes in Express NodeJS](http://stackoverflow.com/questions/12737371/calling-already-defined-routes-in-other-routes-in-express-nodejs) – Yohanes Gultom Mar 06 '17 at 07:16

2 Answers2

1

You can also write 1 handler method and use it in both routes;

function doFacebookSignIn(req, res) {
   // Your code here.
}

or

// Must be defined before using it in code.
var doFacebookSignIn = function(req, res) {
  // Your code here.
}; 

router.get('/facebook', doFacebookSignIn);
router.post('/facebook/sign', doFacebookSignIn);

But as I pointed out in the comments, you should be aware that when using a request body in a GET request, there are some things to consider.

Community
  • 1
  • 1
Nico Van Belle
  • 3,917
  • 4
  • 24
  • 43
1

You cannot redirect GET to POST, a redirect is used to notify an HTTP client that a resource has changed location and should attempt the same request using the new location.

It is noted in the RFC that in some cases POST will be downgraded to GET when the request is re-issued.

  Note: When automatically redirecting a POST request after
  receiving a 301 status code, some existing HTTP/1.0 user agents
  will erroneously change it into a GET request.

If a POST request is expected on the server, then the client should send a POST request rather than a GET request.

Jake Holzinger
  • 4,610
  • 1
  • 14
  • 30