0

I am trying to use node-http-proxy inside an AdonisJS controller, but I get the error

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

The line causing the error is the 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 });

  }

2 Answers 2

0

In the end I got a bit closer but not a full fix. The getting close bit was to realise the http-proxy was delivering data via buffer so I had to do something like this

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){
        }
      })
    }); 

However, I still could not get it to work as the controller was returning before http-proxy had completed the request.

In the end and probably for the best, I wrote a stand alone proxy app and used the main app just to validate JWT tokens before they go through the Proxy.

Sign up to request clarification or add additional context in comments.

Comments

0

You were so close, I wanted to do something similar and wrapped the proxy in a promise so we can wait for the proxy to return before responding with our response:

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;

I thought I'd give you a complete answer for anyone else that comes across this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.