Skip to content

Route Matching Confusion Between blogs/:id and blogs/create #6252

Answered by krzysdz
mitulgupta72 asked this question in Q&A
Discussion options

You must be logged in to vote

If handler for /blogs/create is declared before the parametrized /blogs/:id it will be called first. That's how route matching works in Express - the first matching handler is used. If you can't move more specific routes above the parametrized ones, you can try detecting those routes and skipping to the next matching handler using next().

const express = require("express");

const app = express();

// This route is declared first, so it has priority over parametrized one
app.get('/blogs/create', (req, res) => {
    res.send('Create a blog');
});

app.get('/blogs/:id', (req, res, next) => {
    // Skip this handler if the url contains non-empty "skip" parameter in query
    if (req.query.skip

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by IamLizu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #6244 on January 01, 2025 13:08.