【问题标题】:how to use node-http-proxy inside AdonisJS controller如何在 AdonisJS 控制器中使用 node-http-proxy
【发布时间】:2019-07-01 10:38:13
【问题描述】:

我正在尝试在 AdonisJS 控制器中使用 node-http-proxy,但出现错误

The "url" argument must be of type string. Received type function

导致错误的行是proxy.web(request, response, { target: urlToProxy });

async proxy({ request, response }){
    var resource = await Resource.query().where('uri', request.url()).with('service.user').with('userSecurity').first()
    resource = resource.toJSON()
    if(!resource.service.active){
      return response.status(404).send(`Parent service '${resource.service.title}' is disabled`)
    }
    if(!resource.active){
      return response.status(404).send(`Resource is disabled`)
    }
    if(resource.userSecurity.length <= 0) {
      return response.status(403).send(`You are not permitted to access that resource. Contact ${resource.service.user.first_name} ${resource.service.user.last_name} (${resource.service.user.email})`)
    }

    var urlToProxy = url.resolve(resource.service.basepath, request.originalUrl())
    var proxy = httpProxy.createProxyServer()

    proxy.web(request, response, { target: urlToProxy });

  }

【问题讨论】:

    标签: node.js node-http-proxy adonis.js


    【解决方案1】:

    最后,我离得更近了一点,但还没有完全解决。接近一点是意识到 http-proxy 正在通过缓冲区传递数据,所以我不得不做这样的事情

    proxy.web(req, res, { target: data.service.basepath})
        proxy.on('error', function(err, req, res){
          console.log("There was an error", err)
        })
    
        proxy.on('proxyRes', async (proxyRes, request, response) =>{
          var body = new Buffer('')
          proxyRes.on('data', (data)=>{
            body = Buffer.concat([body, data])
          })
    
          proxyRes.on('end', ()=>{
            body = body.toString()
            try{
              res.send(body)
            }catch(err){
            }
          })
        }); 
    

    但是,我仍然无法让它工作,因为控制器在 http-proxy 完成请求之前返回。

    最后,也许是最好的结果,我编写了一个独立的代理应用程序,并使用主应用程序在 JWT 令牌通过代理之前验证它们。

    【讨论】:

      【解决方案2】:

      你是如此接近,我想做类似的事情并将代理包装在一个承诺中,这样我们就可以等待代理返回,然后再响应我们的响应:

      const proxy = httpProxy.createProxyServer();
      const prom = new Promise((resolve, reject) => {
          proxy.web(request.request, response.response, {
              target: urlToTarget
          }, (e) => {
              reject(e);
          });
      
          proxy.on('proxyRes', function (proxyRes, req, res) {
              let body = [];
              proxyRes.on('data', function (chunk) {
                  body.push(chunk);
              });
              proxyRes.on('end', function () {
                  body = Buffer.concat(body).toString();
                  resolve(body);
              });
          });
      });
      
      const result = await prom;
      response.body(result);
      return response;
      

      我想我会给遇到这个问题的其他人一个完整的答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-22
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        • 2012-03-11
        • 2013-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多