Search code examples
node.jsexpressgatewayexpress-gateway

Express gateway how to ignore path but use rest of the url


My configurations are as below

apiEndpoints:
  api:
    host: '*'
    paths: '/ip'
  approval-engine:
    host: '*'
    paths: '/app/*'
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  approval-engine:
    url: 'http://localhost:8001/'

With proxy as

  - proxy:
      - action:
          serviceEndpoint: approval-engine
          ignorePath: false
          prependPath: false
          autoRewrite : true
          changeOrigin: true

When i make a request to http://localhost:8080/app/category the request is routed to localhost:8001/app/category

My question is can we route the request to http://localhost:8001/category. I want to ignore the paths:/app/ part in proxy.


Solution

  • To accomplish this, you'll need to use the express-gateway rewrite plugin.

    You can use the eg CLI to install the plugin.

    eg plugin install express-gateway-plugin-rewrite
    

    Make sure rewrite is included in the gateway config's policies whitelist.

    In the pipeline that's handling the request, you can use the rewrite plugin like so:

    policies:
      - rewrite:
        - condition:
            name: regexpmatch
            match: ^/app/(.*)$
          action:
            rewrite: /$1
    

    This should remove /app from the path before the request is routed to the Service Endpoint.