gpt4 book ai didi

javascript - ExpressJS 路由无法解析

转载 作者:行者123 更新时间:2023-12-03 01:51:46 26 4
gpt4 key购买 nike

我有一个用 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>

我可以看出响应来自 Express 服务器,因为它具有由服务器设置的自定义响应 header 。我真的很困惑到底出了什么问题,任何帮助将不胜感激。

最佳答案

它对你不起作用,因为你的入口只是重定向请求 https://test.xyz/payments/cards测试服务无需重写路径。所以测试服务仍然变成请求“/payments/cards”,显然无法处理它们。

您可以重新配置 Express 应用程序以支持/payments/cards 或通过 inges '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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com