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
1
  • 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? Commented May 31, 2018 at 14:53

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

1 Comment

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." }

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.