Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

579
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda