使用Node.js的服务器端代理 [英] Server Side Proxy using Node.js

查看:136
本文介绍了使用Node.js的服务器端代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Web应用程序,该应用程序必须对CDAP服务器进行REST调用.当我尝试使用典型的jQuery/AJAX时,由于很明显的原因,那就是跨域请求,因此我遇到了CORS/Access-Control-Allow-Origin问题.遗憾的是,CDAP不支持CORS.

I am trying to create a web application that has to make a REST call to a CDAP Server. When I tried using the typical jQuery/AJAX I was running into CORS/Access-Control-Allow-Origin issue due to the obvious reason that this would be a cross domain request. Sadly, CDAP does not support CORS.

现在剩下的唯一选择是创建服务器端代理.以下是设置:

Now the only option I am left out with is to create a Server Side proxy. The following is the setup:

  • Nodejs为来自浏览器的所有调用公开通用端点(/rest/*).
  • 浏览器对cdap资源的节点代理进行所有调用.

以下是浏览器,Nodejs代理&之间的控制流程. CDAP,

The following is the flow of control between browser, Nodejs proxy & CDAP,

  • 浏览器
    • 对节点代理进行(http)调用.
    • Browser
      • Makes (http) calls to the node proxy.
      • 接收来自浏览器的http调用,并适当地更改url以向CDAP后端发出相同的请求.
      • 从CDAP获得针对上述请求的响应,并将其路由回客户端.

      Nodejs代理在localhost:8500上运行

      Nodejs proxy is running at localhost:8500

      CDAP实例在localhost:10000上运行

      CDAP instance is running at localhost:10000

      浏览器:

      Nodejs代理:

      • Receives this request and modifies the url to - http://localhost:10000/v3/namespaces/default/apps/S3Text and makes a request to CDAP backend.
      • Once it receives a response from the cdap backend, it then re-routes the response to the client.

      此REST调用的等效curl如下:

      The curl equivalent of this REST call is below:

      curl -v localhost:10000/v3/namespaces/default/apps/S3Text -H"Content-Type:application/json" -d @ new.json -X PUT

      我是Node.js的新手,但已经阅读了一些文档并浏览了一些视频.我遇到的问题是我需要加载JSON文件和其他参数(例如方法,数据,dataType等)的哪一部分.它将在JS代码中还是在Node.js代理服务器代码中.如果必须输入nodeProxy.js代码,那么我需要在哪里以及如何传递它们?如果我天真,我会道歉.

      I am new to Node.js but have read some documentation and went through few videos. The point where I am stuck is which part I need to load the JSON file and other parameters like method, data, dataType, etc. Will it be in the JS code or in the Node.js proxy server code. If it has to go in the nodeProxy.js code, then where and how do I need to pass them? My apologies if I am being naive.

      JS代码:

      function sendCurlRequest(){    
          var jsonData = <JSON_DATA>;
          $.ajax({
              cache : false,
              method: "PUT",
              crossDomain: true,
              url: 'http://localhost:8500/rest/v3/namespaces/default/apps/S3Text',
              data: jsonData,
              dataType: "json",
              contentType: "application/json",
              success: function(data){
                      alert("Success");
              },
              error: function(data){
                      alert("Error: " + JSON.stringify(data));
              },
              complete: function(data){
                      console.log("Call Completed");
              }
          });
      }
      

      nodeProxy.js代码:

      nodeProxy.js code:

      var http = require('http');
      var httpRequest = require('request');
      var destinationURL = 'http://localhost:1000/v3/namespaces/default/apps/S3Text';
      
      http.createServer(function (req, res) {
          var options = {
              url: destinationURL
          }
          var destinationResponse =   req.pipe(request(options))destinationResponse.pipe(res)
      }).listen(8500, 'localhost');
      
      console.log('Server running at http://localhost:8500');
      

      推荐答案

      如果您愿意使用某些Node模块,则此方法可能会起作用,尽管我认为它不会将您的URL从localhost:8500/rest/v3更改为localhost:10000/v3 . /rest/将留在其中.

      If you're willing to use some Node modules then this may work though I don't think it will change your URL from localhost:8500/rest/v3 to localhost:10000/v3. The /rest/ will stay in.

      JS

      return $http.get("http://localhost:8500/rest/v3/namespaces/default/apps/S3Text")
      
      return $http.post("http://localhost:8500/rest/v3/namespaces/default/apps/S3Text/other", availReqObj);
      

      节点

      var express = require('express');
      var app = express();
      var cors = require('cors');
      var proxy = require('http-proxy-middleware');
      
      var port = process.env.PORT || 8500;
      
      app.use(express.static("" + __dirname));
      app.use(cors());
      
      app.use('/rest/v3/namespaces/default/apps/S3Text',
          proxy({target: "http://localhost:10000",
                 changeOrigin: false,
                 logLevel: 'debug'
                })
         );
      
      app.use('/rest/v3/namespaces/default/apps/S3Text/other',
         proxy({target: "http://localhost:10000",
                changeOrigin: false,
                logLevel: 'debug'
               })
         );
      
      app.listen(port, function() {
          console.log("Listening at " + port);
      });
      

      这篇关于使用Node.js的服务器端代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆