Express网关如何忽略路径,但使用其余URL

时间:2018-08-03 20:07:05

标签: node.js express gateway express-gateway

我的配置如下

apiEndpoints:
  api:
    host: '*'
    paths: '/ip'
  approval-engine:
    host: '*'
    paths: '/app/*'
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  approval-engine:
    url: 'http://localhost:8001/'

代理为

  - proxy:
      - action:
          serviceEndpoint: approval-engine
          ignorePath: false
          prependPath: false
          autoRewrite : true
          changeOrigin: true

当我向http://localhost:8080/app/category发出请求时,该请求被路由到localhost:8001 / app / category

我的问题是我们可以将请求路由到http://localhost:8001/category吗?我想忽略代理中的path:/ app /部分。

1 个答案:

答案 0 :(得分:2)

要完成此操作,您需要使用express-gateway rewrite plugin

您可以使用eg CLI来安装插件。

eg plugin install express-gateway-plugin-rewrite

确保rewrite包含在网关配置的policies白名单中。

在处理请求的管道中,您可以像这样使用重写插件:

policies:
  - rewrite:
    - condition:
        name: regexpmatch
        match: ^/app/(.*)$
      action:
        rewrite: /$1

这应该在将请求路由到服务端点之前从路径中删除/app