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

150
Visualizações
URL mapping in node js with express
    router.get("customer/:customerId", async (request, response) => {
        console.log("Fetch customer with a particular customer ID")
    }
    
    router.get("customer/regions", async (request, response) => {
        console.log("Fetch all customers from a region")
    }

But whenever I make any request the request is being served by the first api(the regions is getting considered as customerId) and not the second one.How can we have url mappings in these kind of scenarios?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can handle this problem three ways.

One is to switch the order of your two router.get()s. Express handles them in order. The way you have it, express thinks regions is a customerId.

Another is to append a regexp to your :customerId route parameter. For example, if your customerIds are 12-digit numbers you can do this to prevent that route from handling anything except customer/123456654321-style URLs.

router.get("customer/:customerId(\d{12})", async (request, response) => {
  console.log("Fetch customer with a particular customer ID")
}

A third is to use the next() parameter. It tells express that your route handler refuses the URL, and to move on to try other matching routes.

router.get("customer/:customerId(\d{12})", async (req, res, next) => {
  if (<<it's an invalid customerId>>) return next()
  console.log("Fetch customer with a particular customer ID")
}

You probably should use next() anyway to refuse invalid customerIds, and you should use random hard-to-guess customerId values. Because Panera Bread.

about 4 years ago · Juan Pablo Isaza 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