首页 > 解决方案 > 节点快递命中 :id 而不是 404

问题描述

我正在开发一个小的 API 集合来学习nodejsexpress我发现自己处于这种情况。我有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 街区。我正在网上搜索所有答案,但我仍然没有找到明确的解决方案。谁能帮我?

标签: javascriptnode.jsapirestexpress

解决方案


当您使用 a:param作为路径的一部分时,它会匹配像 . 这样的所有内容/*。您可以使用 . 从请求中获取 params 值req.params.param。在您的情况下/imnotreal,与 匹配/:id,因此它最终以:id作为参数命中路线。您可以做的是使用正则表达式来捕获路径模式。因此,如果您的 id 仅包含数字,则不要使用

router.get("/:id"

您可以使用

router.get(/^\/([0-9]+)$/i)

这将匹配带有数字的路径。例如: /123456 而不是 /imnotreal


推荐阅读