Express.js代理发布请求



我查看了节点的请求模块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');
      });
    });
});

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

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

最新更新