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

581
Visualizações
Handling different URL paths while hitting a single endpoint (Nodejs)

I am using express nodejs to hit an endpoint:

app.use("/number", numberRouter);

Now I have to handle URL in which the path is different. Here we have three different paths in the same URL:

  • https://localhost:8080/number/one
  • https://localhost:8080/number/two
  • https://localhost:8080/number/three

The file in which the numberRouter is handled:

numberRouter.post("/", async (req:Request, res:Response) => {
    var url = req.protocol + '://' + req.get('host') + req.originalUrl;

    //what I want to do
    if (req.path == "one") {
        //do something
    } 
    else if (req.path == "two") {
       //do something
    }
});

What I want to achieve is that once I hit the number endpoint, I fetch the full URL, extract out it's path and based on the path I do further processing instead of hitting three different endpoints (/number/one, /number/two, /number/three). Is this possible?

I am using postman for testing and if I send a post request with the following URL: localhost:8080/number/one the post request fails. I want something like this in the code:

numberRouter.post("/variablePath", async (req:Request, res:Response) => { ... }

where the variablePath is set via postman (one, two or three) and then processed here.

SOLUTION (following @traynor's answer):

app.use("/number", numberRouter);
numberRouter.post("/:pathNum", async (req:Request, res:Response) => {
    if (req.path === "/one") {
        //do something
    } 
    else if (req.path === "/two") {
       //do something
    }
});

The post request via postman: localhost:8080/number/:pathNum. In postman set the value of pathNum in the Params section under the Path Variables heading.

enter image description here

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

use route parameters

add your parameter to the router, for example /:myparam, and then check it and run your code:

numberRouter.post("/:myparam", async (req:Request, res:Response) => {
    const myParam = req.params.myparam;

    //what I want to do
    if (myParam == "one") {
        //do something
    } 
    else if (myParam == "two") {
       //do something
    }

});
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