最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to Resolve Route Conflicts in Express.js with Similar Route Paths? - Stack Overflow

programmeradmin1浏览0评论

I'm building a URL shortener API using Express.js, and I'm encountering a route conflict issue. Here are the relevant parts of my setup:

router.get("/", getAllUrls);
router.get("/:id", getUrlById);
router.delete("/:id", deleteUrlById);
router.post("/", shortenUrl);
router.get("/:shortCode", redirectToOriginalUrl); 

When I visit the route to redirect based on the shortCode (e.g., http://localhost:5001/oWBI), the getUrlById handler gets called instead, causing errors. The route /:id seems to conflict with /:shortCode.

Expected Behavior: /oWBI should trigger the redirectToOriginalUrl function. /12345 (example ID) should trigger the getUrlById function.

Current Error: The server attempts to process /:id for both cases, leading to errors when it cannot find an ID.

What I Tried: I tried rearranging the route definitions but couldn't achieve the desired behavior. Here's the error I receive when visiting http://localhost:5001/oWBI:

I'm building a URL shortener API using Express.js, and I'm encountering a route conflict issue. Here are the relevant parts of my setup:

router.get("/", getAllUrls);
router.get("/:id", getUrlById);
router.delete("/:id", deleteUrlById);
router.post("/", shortenUrl);
router.get("/:shortCode", redirectToOriginalUrl); 

When I visit the route to redirect based on the shortCode (e.g., http://localhost:5001/oWBI), the getUrlById handler gets called instead, causing errors. The route /:id seems to conflict with /:shortCode.

Expected Behavior: /oWBI should trigger the redirectToOriginalUrl function. /12345 (example ID) should trigger the getUrlById function.

Current Error: The server attempts to process /:id for both cases, leading to errors when it cannot find an ID.

What I Tried: I tried rearranging the route definitions but couldn't achieve the desired behavior. Here's the error I receive when visiting http://localhost:5001/oWBI:

Share Improve this question edited Nov 17, 2024 at 5:25 aneroid 16.4k3 gold badges40 silver badges75 bronze badges asked Nov 17, 2024 at 1:12 Alif LaksonoAlif Laksono 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The issue is very simple, Let me break down what is happening here. So, when you call http://localhost:5001/oWBI, this matches with /:id route syntax.

So, You need to make the /:id route more specific so that it does not conflict with /:shortCode. There are two common approaches:

  1. Use a Prefix for the /:id Route. This goes something like this:
router.get("/", getAllUrls);
router.get("/get/:id", getUrlById); // a prefix before /:id to make it more specific
router.delete("/delete/:id", deleteUrlById); // same here too
router.post("/", shortenUrl);
router.get("/:shortCode", redirectToOriginalUrl);
  1. Use a Validation Middleware for /:id Or, use a middleware to differentiate between valid IDs and short codes. For instance, if IDs are numeric and short codes are alphanumeric, you can handle this dynamically:
router.get("/:param", (req, res, next) => {
    const { param } = req.params;

    if (isNumeric(param)) {
        return getUrlById(req, res, next); // now we call our getUrlById which returns item by id
    } else {
        return redirectToOriginalUrl(req, res, next); // or we redirect to other url.
    }
});

P.S: This is a very simple code i wrote in order to explain. If you want to go with this middleware approach you would have to research in depth and handle more edge cases.

发布评论

评论列表(0)

  1. 暂无评论