解析参数以表达端点

时间:2018-05-31 13:06:24

标签: node.js express express-gateway

我运行了几个正在查询数据库的快速端点。我正在尝试解析我的快速网关中的参数,如下所示:

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

用户端点在localhost上运行,/users确实按预期返回所有用户。问题是/users/:userId也会返回所有用户 - 它应该只返回一个。

当我尝试在没有网关的情况下调用端点时,它正常工作(http://localhost:3000/users/F692D717-F304-4D9B-A302-44F143923A93/

但它没有通过网关工作。它似乎永远不会到达最后一个端点或者不解析参数。

我的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

2 个答案:

答案 0 :(得分:0)

您是否尝试将ignorePath设为false?这应该是技巧,或者只是从文件中删除选项,因为它的默认值是false

答案 1 :(得分:0)

找到解决方案。在给定端点的代理策略操作下,我需要设置prependPath:false。我似乎无法在文档中找到原因。

最终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