Using node-http-proxy in a controller

I am trying to use node-http-proxy inside a 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 });
    
  }
1 Like

The node-http-proxy would need the actual request and response objects of Node.js HTTP server, which you can access as follows:

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

Thanks Virk, I worked out what I missing in the end. Most software is not documented as well as Adonis is :slight_smile: