Search code examples
node.jsexpressexpress-gateway

Parsing parameters to express endpoint


I have several express endpoints running which are querying a database. I'm trying to parse parameters in my express-gateway like this:

paths: ['/users', '/users/:userId']

The user endpoint is running on localhost and /users does return all users as intended. The problem is that /users/:userId also returns all users - it should only return one.

When i try to call the endpoint without the gateway it is working fine (http://localhost:3000/users/F692D717-F304-4D9B-A302-44F143923A93/)

But it's not working through the gateway. It seems like it never reaches the last endpoint or doesn't parse the parameter.

My gateway.config.yml:

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  users:
    host: "*"
    paths: ['/users', '/users/:userId']
  accounts:
    host: "*"
    paths: '/accounts'
  companies:
    host: "*"
    paths: '/companies'
serviceEndpoints:
  users:
    url: 'http://localhost:3000/users'
  accounts:
    url: 'http://localhost:3002/accounts'
  companies:
    url: 'http://localhost:3001/companies'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  users:
    apiEndpoints:
      - users
    policies:
      - proxy:
          - action:
              serviceEndpoint: users 
              changeOrigin: false
              ignorePath: true
  accounts:
    apiEndpoints:
      - accounts
    policies:
      - proxy:
          - action:
              serviceEndpoint: accounts 
              changeOrigin: false
              ignorePath: true
  companies:
    apiEndpoints:
      - companies
    policies:
      - proxy:
          - action:
              serviceEndpoint: companies 
              changeOrigin: false
              ignorePath: true

Solution

  • Found a solution. Under the proxy policy action for a given endpoint i need to set prependPath:false. I can't seem to find a reason inside the documentation.

    Final gateway.config.yml:

     http:
      port: 8080
    admin:
      port: 9876
      hostname: localhost
    apiEndpoints:
      users:
        host: "*"
        paths: ['/users', '/users/:userId']
      accounts:
        host: "*"
        paths: '/accounts'
      companies:
        host: "*"
        paths: '/companies'
    serviceEndpoints:
      users:
        url: 'http://localhost:3000/users'
      accounts:
        url: 'http://localhost:3002/accounts'
      companies:
        url: 'http://localhost:3001/companies'
    policies:
      - basic-auth
      - cors
      - expression
      - key-auth
      - log
      - oauth2
      - proxy
      - rate-limit
    pipelines:
      users:
        apiEndpoints:
          - users
        policies:
          - proxy:
              - action:
                  serviceEndpoint: users 
                  changeOrigin: false
                  prependPath: false
      accounts:
        apiEndpoints:
          - accounts
        policies:
          - proxy:
              - action:
                  serviceEndpoint: accounts 
                  changeOrigin: false
                  prependPath: false
      companies:
        apiEndpoints:
          - companies
        policies:
          - proxy:
              - action:
                  serviceEndpoint: companies 
                  changeOrigin: false
                  prependPath: false