ExpressJS路由无法解析

时间:2018-05-17 10:11:49

标签: javascript typescript express kubernetes-helm kubernetes-ingress

我在AKS上的kubernetes集群中运行了一个用typescript编写的小型微服务。 我使用Helm生成入口

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-service
  labels:
    app: test-service
    chart: test-service-0.1.0
    release: test-service
    heritage: Tiller
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
spec:
  tls:
    - hosts:
        - test.xyz
      secretName: default-tls
  rules:
    - host: test.xyz
      http:
        paths:
          - path: /payments
            backend:
              serviceName: test-service
              servicePort: 4040

服务

apiVersion: v1
kind: Service
metadata:
  name: existing-bumblebee-payments-service
  labels:
    app: test-service
    chart: test-service-0.1.0
    release: existing-bumblebee
    heritage: Tiller
spec:
  type: ClusterIP
  ports:
    - port: 4040
      targetPort: 4040
      protocol: TCP
  selector:
    app: test-service
    release: existing-bumblebee

在我的微服务中

export class Server {
private registerRoutes() {
        this.app.use("/cards", CardRouter);
        this.app.use("/wallets", WalletRouter);
        this.app.use("/preauth", PreauthRouter);
        this.app.use("/charge", ChargeRouter);
    }
}

路由器看起来像这样;

    import { Router } from "express";
    // other imports
    import { checkSchema, check } from "express-validator/check";

    const router = Router();

    router.get("/", CardController.index);
    router.get("/:id", [
        check("id")
            .isUUID()
            .withMessage("Invalid UUID")
    ], CardController.get);
    router.delete("/:id", [
        check("id")
            .isUUID()
            .withMessage("Invalid UUID")
    ], CardController.remove);
    router.post("/", checkSchema(CardCreateRules), CardController.add);

export default router;

但是,通过https://test.xyz/payments/cards访问服务会导致来自快递的错误

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Cannot GET /payments/cards</pre>
    </body>
</html>

我可以告诉响应来自快速服务器,因为它具有服务器设置的自定义响应标头。 我真的很困惑什么是错的,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

它不适合您,因为您的入口只是将请求https://test.xyz/payments/cards重定向到测试服务而不重写路径。因此测试服务仍然成为请求“/ payment / cards”,显然无法处理它们。

您可以将快速应用程序重新配置为支持/付款/卡片,也可以通过ingess'nginx.ingress.kubernetes.io/rewrite-target'注释重写路径:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-service
  labels:
    app: test-service
    chart: test-service-0.1.0
    release: test-service
    heritage: Tiller
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /payments
spec:
  tls:
    - hosts:
        - test.xyz
      secretName: default-tls
  rules:
    - host: test.xyz
      http:
        paths:
          - path: /
            backend:
              serviceName: test-service
              servicePort: 4040