javascript - 节点快递命中 :id 而不是 404
问题描述
我正在开发一个小的 API 集合来学习nodejs
,express
我发现自己处于这种情况。我有app.use("/v1/phrases", phraseRouter);
在路由器中,我有以下代码:
const router = require("express").Router();
const jwtHandler = require("../../auth/jwt-module");
const controller = require("./phrases.controller");
router.get("/hash", jwtHandler.authenticateJwt, async (req, resp) => {
try {
const hash = await controller.getCollectionHash();
resp.status(200).send(hash);
} catch (error) {
console.log("error on /phrases/hash =>", error);
resp.status(500).send(error);
}
});
router.get("/", jwtHandler.authenticateJwt, async (req, resp) => {
try {
const phrases = await controller.listPhrases();
resp.status(200).send(phrases);
} catch (error) {
console.log("error on /phrases =>", error);
resp.status(500).send(error);
}
});
router.get("/:id", jwtHandler.authenticateJwt, async (req, resp) => {
const phraseId = req.params.id;
try {
const phrase = await controller.getPhrase(phraseId);
resp.status(200).send(phrase);
} catch (error) {
if (error.code) {
console.log("error on /phrases/:id =>", error);
resp.status(error.code).send(error.msg);
} else {
resp.status(500).send(error);
}
}
});
router.post("/", jwtHandler.authenticateJwt, async (req, resp) => {
try {
const phrase = await controller.addNewPhrase(req.body);
resp.status(201).send(phrase);
} catch (error) {
if (error.code) {
console.log("error on post to /phrases =>", error);
resp.status(error.code).send(error.msg);
} else {
resp.status(500).send(error);
}
}
});
router.put("/:id", jwtHandler.authenticateJwt, async (req, resp) => {
const phraseId = req.params.id;
try {
await controller.updatePhrase(phraseId, req.body);
resp.sendStatus(204);
} catch (error) {
if (error.code) {
console.log("error on put to /phrases/:id =>", error);
resp.status(error.code).send(error.msg);
} else {
resp.status(500).send(error);
}
}
});
router.delete("/:id", jwtHandler.authenticateJwt, async (req, resp) => {
const phraseId = req.params.id;
try {
await controller.deletePhrase(phraseId);
resp.sendStatus(204);
} catch (error) {
if (error.code) {
console.log("error on delete at /phrases/:id =>", error);
resp.status(error.code).send(error.msg);
} else {
resp.status(500).send(error);
}
}
});
router.get("*", (req, resp, next) => {
console.log("hit this");
});
module.exports = router;
出于某种我无法理解的原因,每当我输入一条不存在的路线时,就像/v1/phrases/imnotreal
我最终打的:id
是 404 而不是我的 404 街区。我正在网上搜索所有答案,但我仍然没有找到明确的解决方案。谁能帮我?
解决方案
当您使用 a:param
作为路径的一部分时,它会匹配像 . 这样的所有内容/*
。您可以使用 . 从请求中获取 params 值req.params.param
。在您的情况下/imnotreal
,与 匹配/:id
,因此它最终以:id
作为参数命中路线。您可以做的是使用正则表达式来捕获路径模式。因此,如果您的 id 仅包含数字,则不要使用
router.get("/:id"
您可以使用
router.get(/^\/([0-9]+)$/i)
这将匹配带有数字的路径。例如:
/123456
而不是
/imnotreal
推荐阅读
- macos - QEMU .qcow2 mmu_gva_to_gpa 在 Mac OS X 中崩溃
- python - 使用python用变量替换值?
- python - 与 np.where 不匹配的系列长度
- javascript - 提取值
- c# - 运行 Async/Await 实验时,操作未按预期顺序发生
- qt - 如何使用cmake强制UIC在源目录中生成ui_mainwindow.h
- angular - (角度自定义验证)输入字段始终具有以前的值而不是更新的值
- angular - 在 ng-select 中禁用自动关闭
- r - 查询结果为空时的消息
- apache-kafka - Kafka 使用 logstash 流式传输到 cassandra