简体   繁体   中英

Building a “Filter Proxy” in Node.js

I have an elasticsearch Server which I would like to query, but before showing the result to the User I want to filter the results (looking up the rights for the User in a Database etc.)

So I thought I write a proxy Server which recieves JSON POST Search request and redirect this to the Elasticsearch Server. The response with the results now be sent to the "filter server". This Server looks the recieved json-data up in a Database and removes the results which the User isn't allowed to see. This filtered content should be presented to the user.

Ok - this is what i've done:

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)

This is the proxy Server but without the part where the results are filtered by the filtering instance. I tried lots of things, but couldn't get it to work as I want it to. I hope somebody can help me with this!

This is what you need:

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

This is a simple but useful elasticsearch proxy, built on node.js, the proxy allow to intercept and modify requests and responses, by defining two functions:

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

Take a look to this example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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