Express网关提供404

时间:2017-12-14 11:07:43

标签: node.js express-gateway

我是服务网址 获取请求http://myipaddress:5000/api/Tenant/tenants/{TenantID}

TenantID将是动态的

我的POST也是http://myipaddress:5000/api/Tenant/tenants

在此帖子中,请求有效负载在请求正文中传递。

我的网关配置yml文件如下

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  tenant-api:
    host: localhost
    paths: '/api/Tenant/tenants/*'

serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  tenant-svc:
    url: 'http://localhost:5000'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
      - tenant-api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true

当我尝试通过代理执行GET请求时,我得到404。您也可以告诉我如何在gatewayconfig.yml中添加POST api端点

1 个答案:

答案 0 :(得分:2)

第一个问题是您在代理政策中有多项操作

- action:  # this one is always executing and goes to the httpbin
      serviceEndpoint: httpbin 
      changeOrigin: true
- action:
      serviceEndpoint: tenant-svc 
      changeOrigin: true

删除第一个操作以使所有调用转到tenant-svc

- action:
      serviceEndpoint: tenant-svc 
      changeOrigin: true

此配置将接受所有方法GET,POST上'/ api / Tenant / tenants / *'网址

使Express-Gateway进程/ api / Tenant / tenants url你可以修改api端点,如:

paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 

https://www.express-gateway.io/docs/configuration/gateway.config.yml/apiEndpoints#markdown

我认为不需要特殊处理GET POST。 如果您需要Gateway只过滤特定方法,您可以添加

methods: 'POST,PUT' 

到你的api端点配置

所以最终配置看起来像

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  tenant-api:
    host: localhost
    methods: 'GET,POST,PUT' 
    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 

serviceEndpoints:
  tenant-svc:
    url: 'http://localhost:5000'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - tenant-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true

或者您可以使用方法

将多个API端点连接到同一个管道
  tenant-api-1:
    host: localhost
    methods: 'GET' 
    paths: '/api/Tenant/tenants/*'

  tenant-api-2:
    host: localhost
    methods: 'POST' 
    paths:  '/api/Tenant/tenants' 

更新:多服务使用

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  tenant-api:
    host: localhost
    methods: 'GET,POST,PUT' 
    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 
  product-api:
    host: localhost
    paths: ['/api/products/*'] 
serviceEndpoints:
  tenant-svc:
    url: 'http://localhost:5000'
  product-svc:
    url: 'http://localhost:6000'
policies:
  - proxy
pipelines:
  tenant:
    apiEndpoints:
      - tenant-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true
  products:
    apiEndpoints:
      - product-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: product-svc 
              changeOrigin: true