我有一个用Typescript编写的小型微服务,该微服务在AKS上的kubernetes集群中运行。
我已经使用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
访问服务会导致来自Express的404错误<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /payments/cards</pre>
</body>
</html>
我可以说响应来自快递服务器,因为它具有服务器设置的自定义响应头。
对于什么地方出了问题我真的很困惑,我们将不胜感激。
最佳答案
它对您不起作用,因为您的入口仅将请求https://test.xyz/payments/cards重定向到测试服务,而无需重写路径。因此测试服务仍然成为请求“ /支付/卡”,显然无法处理它们。
您可以重新配置快递应用程序以支持/ payments / 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
关于javascript - ExpressJS路由无法解析,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50388852/