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

标签 javascript json node.js curl proxy

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

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

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

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

  • 浏览器
    • 对 Node 代理进行 (http) 调用。
  • Nodejs 服务器
    • 接收来自浏览器的 http 调用并适当更改 URL,以向 CDAP 后端发出相同的请求。
    • 从 CDAP 获取对上述请求的响应,并将其路由回客户端。

Nodejs 代理正在 localhost:8500 运行

CDAP 实例正在 localhost:10000 运行

浏览器:

Nodejs 代理:

此 REST 调用的等效curl如下:

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

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

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 代码:

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/ 将保留在其中。

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

关于javascript - 使用 Node.js 的服务器端代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34685030/

相关文章:

javascript - jQuery 在 DOM 中定位 facebook 元素

javascript - 从经纬度查找一定范围内的一组坐标

sql - 将 JSON 文件保存到 SQL Server 数据库表

node.js - NodeJS、Passport 和 Facebook - 为什么我无法注销

javascript - 多次调用函数后redis连接丢失

Node.js https pem 错误 : error:0906D06C:PEM routines:PEM_read_bio:no start line

javascript - 使用 Redux 和 Saga 将状态保留到本地存储

javascript - 使用 JavaScript 和 Node.js (Meteor) 循环检索 pdf 发票

python - 如何将 Pandas Dataframe 转换为所需的 Json 格式

php - 表值没有变化