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: