Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Express.js代理post请求_Javascript_Ajax_Node.js_Express - Fatal编程技术网

Javascript Express.js代理post请求

Javascript Express.js代理post请求,javascript,ajax,node.js,express,Javascript,Ajax,Node.js,Express,我检查了节点的请求模块 但不知道如何将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 da

我检查了节点的请求模块 但不知道如何将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
   }
})

伟大的如果答案有帮助,请随意接受:)。我在上面加了这些。你能给我一些建议吗?我会非常感激的:)这不是通过身体,我错过了什么吗?