【问题标题】:Express.js proxy post requestExpress.js 代理发布请求
【发布时间】:2013-12-24 10:42:20
【问题描述】:

我查看了节点 https://github.com/mikeal/request 的请求模块 但无法弄清楚如何将 POST 请求代理到远程服务器,例如

app.post('/items', function(req, res){
  var options = {
      host: 'https://remotedomain.com',
      path: '/api/items/,
      port: 80
    };
    var ret = res;
    http.get(options, function(res){
      var data = '';
      res.on('data', function(chunk){
        data += chunk;    
      });

      res.on('end', function(){
        var obj = JSON.parse(data);
        ret.json({obj: obj});
        console.log('end');
      });
    });
});

【问题讨论】:

    标签: javascript ajax node.js express


    【解决方案1】:

    除非我从你的问题中遗漏了什么,你可以只做一个简单的帖子,然后对响应数据做一些事情:

    var request = require('request');
    
    app.post('/items', function(req, res){
    
       request.post('https://remotedomain.com/api/items', function (error, response, body) {
    
       if (!error && response.statusCode == 200) {
         console.log(body); // Print the body of the response. If it's not there, check the response obj
         //do all your magical stuff here
       }
    })
    

    【讨论】:

    • 太棒了。如果有帮助,请随时接受答案:)。我在上面添加了。
    • 能否请您给stackoverflow.com/questions/22684644/…一些建议,我将不胜感激:)
    • 这不是通过身体,我错过了什么吗?
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 2021-03-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多