繁体   English   中英

Express.js代理发布请求

[英]Express.js proxy post request

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM