0

我有几个正在运行的快速端点正在查询数据库。我正在尝试像这样解析我的 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
4

2 回答 2

1

找到了解决方案。在给定端点的代理策略操作下,我需要设置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
于 2018-06-05T06:39:37.453 回答
0

您是否尝试设置ignorePathfalse?这应该可以解决问题,或者,只需从文件中删除该选项,因为它的默认值为false

于 2018-06-04T13:39:11.027 回答