繁体   English   中英

在Node.js中构建“过滤器代理”

[英]Building a “Filter Proxy” in Node.js

我有一个弹性查询服务器,我想查询它,但是在将结果显示给用户之前,我想过滤结果(在数据库中查找用户的权限等)。

所以我想我写了一个代理服务器,该服务器接收JSON POST搜索请求并将其重定向到Elasticsearch服务器。 现在将带有结果的响应发送到“过滤器服务器”。 该服务器在数据库中查找接收到的json数据,并删除不允许用户查看的结果。 此过滤后的内容应呈现给用户。

好的-这就是我所做的:

var proxy = http.createServer(function (req, res){
  if(req.method == 'OPTIONS'){
   res.writeHead(200, {'Access-Control-Allow-Origin': '*', 'Content-Type':   'application/json; charset=UTF-8'});
   res.end();
 }

if(req.method == 'POST'){

  var searchOptions = {
    host: '10.0.10.1',
    port: 9200,
    method: 'POST',
    path: '/ltg_5096/_search'
  }

    var searchRequest = http.request(searchOptions, function(searchResponse){

    // this is the Request to the Elasticsearch Server...

    var filterOptions = {
      host: '127.0.0.1',
      port: 8080,
      method: 'POST',
      path: '/',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }

    var filterRequest = http.request(filterOptions, function(filterResponse){
      // ?! This should be the request to the filter Server
    })

    searchResponse.pipe(res)
    res.writeHead(200, {'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json; charset=UTF-8'})
  })
  req.pipe(searchRequest)
 }
})

proxy.listen(9000)

这是代理服务器,但没有过滤实例过滤结果的部分。 我尝试了很多事情,但无法按我的意愿去工作。 我希望有人可以帮助我!

这是您需要的:

https://github.com/lukas-vlcek/node.es

这是一个简单但有用的elasticsearch代理,它基于node.js构建,该代理通过定义两个功能允许拦截和修改请求和响应:

var preRequest = function(request) {};
var postRequest = function(request, response, responseData){};
var proxyServer = proxyFactory.getProxy(preRequest, postRequest);
proxyServer.start();

看一下这个例子:

https://github.com/lukas-vlcek/node.es/blob/master/proxy/proxy-example.js

暂无
暂无

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

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