Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda