0

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
Yan
  • 433
  • 2
  • 16
TietjeDK
  • 1,167
  • 2
  • 15
  • 43
  • I use express in my app and I did something similar to you. I used the regex way for that and did it with only one route with this param '/db/user/(:newUser)?'. Could you try? – Rémi C. May 31 '18 at 14:53

2 Answers2

1

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
TietjeDK
  • 1,167
  • 2
  • 15
  • 43
  • `prependPath` defaults to `true`. What this means is that the Service Endpoint URL path will be prefixed to the incoming request path. With `prependPath` equal to `true`, an incoming API Endpoint request to `/users/12345` with a Service Endpoint of `http://localhost:3000/users` will proxy to `http://localhost:3000/users/users/12345`. Setting `prependPath` to `false` ignores the Service Endpoint path altogether. Alternatively, you could remove the `prependPath: false` declaration and remove the paths from your Service Endpoints. That should work just fine. – Kevin Swiber Aug 08 '18 at 04:00
0

Have you tried to set ignorePath to false? That should do the trick or alternatively, just remove the option at all from the file since its default value is false

Vincenzo
  • 1,549
  • 1
  • 9
  • 17
  • If i set `ignorePath: false` i get this error when trying to access my api endpoint: `{ "success": false, "message": "Conversion failed when converting from a character string to uniqueidentifier." }` – TietjeDK Jun 05 '18 at 06:26