简体   繁体   English

使用Node.js的服务器端代理

[英]Server Side Proxy using Node.js

I am trying to create a web application that has to make a REST call to a CDAP Server. 我正在尝试创建一个Web应用程序,该应用程序必须对CDAP服务器进行REST调用。 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. 当我尝试使用典型的jQuery / AJAX时,由于明显的原因(这将是跨域请求),我遇到了CORS / Access-Control-Allow-Origin问题。 Sadly, CDAP does not support CORS. 遗憾的是,CDAP不支持CORS。

Now the only option I am left out with is to create a Server Side proxy. 现在,唯一剩下的选择就是创建服务器端代理。 The following is the setup: 以下是设置:

  • Nodejs exposes a generic end point (/rest/*) for all calls from the browser. Nodejs为来自浏览器的所有调用公开通用端点(/ rest / *)。
  • Browser making all calls to the node proxy for cdap resources. 浏览器对cdap资源的节点代理进行所有调用。

The following is the flow of control between browser, Nodejs proxy & CDAP, 以下是浏览器,Nodejs代理和CDAP之间的控制流程,

  • Browser 浏览器
    • Makes (http) calls to the node proxy. 对节点代理进行(http)调用。
  • Nodejs server Nodejs服务器
    • Receives the http calls from browser and changes the url appropriately to make the same request to CDAP backend. 接收来自浏览器的http调用,并适当地更改url以向CDAP后端发出相同的请求。
    • Gets a response from CDAP for the above request and routes it back to the client. 从CDAP获得对以上请求的响应,并将其路由回客户端。

Nodejs proxy is running at localhost:8500 Node.js代理在本地主机上运行:8500

CDAP instance is running at localhost:10000 CDAP实例正在localhost:10000运行

Browser: 浏览器:

Nodejs proxy: Node.js代理:

The curl equivalent of this REST call is below: 该REST调用的curl等效项如下:

curl -v localhost:10000/v3/namespaces/default/apps/S3Text -H "Content-Type: application/json" -d @new.json -X PUT curl -v本地主机:10000 / v3 /命名空间/ default / apps / S3Text -H“内容类型:application / json” -d @ new.json -X PUT

I am new to Node.js but have read some documentation and went through few videos. 我是Node.js的新手,但已阅读一些文档,并看了一些视频。 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. 卡住的地方是我需要加载JSON文件和其他参数(如方法,数据,dataType等)的哪一部分。它将在JS代码中还是在Node.js代理服务器代码中。 If it has to go in the nodeProxy.js code, then where and how do I need to pass them? 如果必须输入nodeProxy.js代码,那么我需要在哪里以及如何传递它们? My apologies if I am being naive. 如果我天真,我会道歉。

JS Code: 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 code: nodeProxy.js代码:

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');

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 . 如果您愿意使用某些Node模块,那么这可能会起作用,尽管我认为它不会将您的URL从localhost:8500/rest/v3更改为localhost:10000/v3 The /rest/ will stay in. /rest/将保留。

JS 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);

Node 节点

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

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

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