簡體   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