Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Express route declaration

I have 2 routes such as,

router.get("/project/:id", (req,res) => {

console.log(1);

});

router.get("/project/active", (req,res) => {

console.log(2);

})

Whenever I call the /project/active route, /project/:id route is getting called.

Any tips here would help.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your wildcard route with :id in it is greedy and is matching everything including the /project/active URL.

In this route definition:

router.get("/project/:id", ...)

The :id part is a wildcard. It matches ANYTHING. So, /project/anything will match that, including /project/active. Since routes are matched in Express in the order they are declared, your router.get("/project/:id", ...) route matches first and takes the request.

There are four ways around this:

  1. Don't design or declare conflicting routes that might both match a particular URL. In this particular case, you would change one of your two URLs to something unique such as /project/id/:id and /project/active or /project/:id and /activeproject. Then, these two route handlers will never conflict.

  2. Very carefully order your routes from the most specific to the least specific. In this case, you would put router.get("/project/active", ...) before router.get("/project/:id", ...). That gives the more specific route a chance to match before the wildcard route gets to take the request. But, note that this means that your id value can never be named active.

  3. If your ids are uniquely identifiable and different from strings like active (like suppose an ID is all numbers or always contains some numbers), then you can code a regex for the ID that would only match your id values and would not match regular words like `active.

  4. Code your wildcard route to individually check for things it should not match and call next() to continue routing to other routes.

over 4 years ago · Santiago Trujillo Report

0

That happens because you specify a dynamic route before a specific route.

The right one should be:

router.get("/project/active", (req,res) => {
  console.log(2);
});

router.get("/project/:id", (req,res) => {
  console.log(1);
});

In Node.js, we know something that is called a middleware stack. Basically, your requests will pass through middlewares until something gives out a response. Routers are also examples of middlewares.

In this case, your route assumes that /active is an id, as you defined /:id first before /active. Different things (should be your expected behavior) would happen if you switched the order.

References:

  • Middleware Stack
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!