【问题标题】:How to "pass forward" a post() req to another api , get the res and send it back?如何将 post() req“转发”到另一个 api,获取 res 并将其发回?
【发布时间】:2017-03-23 09:32:44
【问题描述】:

背景: 我正在使用构建一个使用 2 个不同的 3rd 方来做某事的系统。 第 3 方 #1 - 是 facebook messenger 应用程序,它需要一个 webhook 来通过 POST() 协议连接和发送信息。 第 3 方 #2 - 是我用来构建机器人的平台(称为 GUPSHUP)。

我的服务器位于它们之间 - 所以,我需要将 facebook messenger 应用程序连接到我服务器上的端点(已经这样做了),因此 Facebook 应用程序收到的每条消息都会发送到我的服务器。

现在,我真正需要的是,我的服务器充当“中间件”,只需将“req”和“res”发送到另一个平台 url(我们称之为 GUPSHUP-URL),获取 res返回并将其发送到 Facebook 应用程序。

我不确定如何编写这样的中间件。 我的服务器发布功能是:

    app.post('/webhook', function (req, res) {
/* send to the GUPSHUP-URL , the req,res which I got ,
   and get the update(?) req and also res so I can pass them
   back like this (I think) 
   req = GUPSHUP-URL.req
   res = GUPSHUP-URL.res
   
*/

});

【问题讨论】:

    标签: node.js express facebook-graph-api middleware


    【解决方案1】:

    是的,您可以使用请求模块在另一台服务器上传递执行请求

    var request = require('request');
    
    app.post('/webhook', function (req, res) {
        /* send to the GUPSHUP-URL , the req,res which I got ,
           and get the update(?) req and also res so I can pass them
           back like this (I think) 
           req = GUPSHUP-URL.req
           res = GUPSHUP-URL.res
    
           */
    
           request('GUPSHUP-URL', function (error, response, body) {
            if(error){
    
                 console.log('error:', error); // Print the error if one occurred 
                 return res.status(400).send(error)
               }
                console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
    
                  console.log('body:', body); // Print the HTML for the Google homepage. 
                  return res.status(200).send(body); //Return to client
                });
    
         });
    

    第二版

    var request = require('request');
    
    
    //use callback function to pass uper post
    function requestToGUPSHUP(url,callback){
    
     request(url, function (error, response, body) {
    
      return callback(error, response, body);
    }
    
    app.post('/webhook', function (req, res) {
        /* send to the GUPSHUP-URL , the req,res which I got ,
           and get the update(?) req and also res so I can pass them
           back like this (I think) 
           req = GUPSHUP-URL.req
           res = GUPSHUP-URL.res
    
           */
    
           requestToGUPSHUP('GUPSHUP-URL',function (error, response, body) {
    
            if(error){
    
              return res.status(400).send(error)
            }
    
              //do whatever you want
    
    
              return res.status(200).send(body); //Return to client
            });
    
    
         });
    

    更多信息Request module

    【讨论】:

    • 感谢@Love-Kesh,感谢您的帮助。抱歉,只是为了确保我理解正确 - 你的请求代码应该是 request('GUPSHUP-URL', function (error, res, req.body) { if (error) {...} /* how do I " return" res.send(200) 到上面的 post()?*/ });
    • 查看我的第二版答案
    • 谢谢@Love-Kesh,你成功了:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 2021-09-30
    • 2017-05-07
    • 2020-04-26
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多