node.js - 在 Node.js 中构建 "Filter Proxy"

标签 node.js elasticsearch

我有一个我想查询的elasticsearch服务器,但在向用户显示结果之前我想过滤结果(在数据库等中查找用户的权限)

所以我想我编写一个代理服务器来接收 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

关于node.js - 在 Node.js 中构建 "Filter Proxy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17617223/

相关文章:

javascript - 如何使用 Node 的调试模块?

html - 如何将普通输入 Div Post 数据发送到服务器

elasticsearch - 使用Elasticsearch的整数的语音搜索结果

elasticsearch - 定义 _parent 字段的路径

带过滤器的 ElasticSearch function_score 查询

node.js - 在整个应用程序中使用单个数据库连接?

node.js - npm 不会在本地安装包。怎么了?

node.js - Node.js 中的颜色 Winston 记录器 v3.x

elasticsearch - 我可以查询特定 Elasticsearch 分片中的文档吗?

elasticsearch - Log4j2编码问题