Express.js代理发布请求

时间:2013-12-06 22:34:45

标签: javascript ajax node.js express

我查看了节点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');
      });
    });
});

1 个答案:

答案 0 :(得分:3)

除非我遗漏了你的问题,否则你可以做一个简单的帖子,然后对回复数据做点什么:

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
   }
})