【问题标题】:Redirecting get to post? res.redirect to POST重定向获取帖子? res.redirect 到 POST
【发布时间】:2017-03-06 06:47:00
【问题描述】:

GET/facebook路由中,我收到来自前端的授权码后,我必须向/facebook/signin发出POST请求

如何将GET /facebook 重定向到POST /facebook/signin

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

POST路由

router.post('/facebook/sign', (req, res, next) => {
    console.log(req.query.code);
    // i will do mine stuff....
});

【问题讨论】:

标签: javascript node.js express


【解决方案1】:

您也可以编写 1 个处理程序方法并在两个路由中使用它;

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

// 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);

但正如我在 cmets 中指出的,您应该注意,在 GET 请求中使用请求正文时,there are some things to consider

【讨论】:

    【解决方案2】:

    您不能将 GET 重定向到 POST,重定向用于通知 HTTP 客户端资源已更改位置,并应使用新位置尝试 same 请求。

    RFC 中指出,在某些情况下,当重新发出请求时,POST 会降级为GET

      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.
    

    如果服务器上需要POST 请求,那么客户端应该发送POST 请求而不是GET 请求。

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 2017-02-05
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      相关资源
      最近更新 更多