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

184
Vistas
Extract the common query path from RESTFUL URL (Ablate the resources) JS

I have a RESTFUL url design like this: GET /street/:streetName/house/:houseNumber or GET /street/:streetName/house/listings. As you can see, the :streetName and :houseNumber are resource names. I am trying to extract the static (common) parts from the url for some following logics which means I want to get /street/house and /street/house/listings (ablate all resources parts in the url).

I was trying to find a JS lib for this but didn't find one. Any pointers?

PS: I can do some string matching to achieve this like split by "/" then concat them and only care about the key words, so I can ignore all resource names. But this doesn't seem robust.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Assuming you have a route for each URL pattern, you can set an additional property req.staticParts in each middleware:

app.get("/street/:streetName/house/:houseNumber", function(req, res, next) {
  req.staticParts = "/street/house";
  ...
})
...
.get("/street/:streetName/house/:houseNumber/listings", function(req, res, next) {
  req.staticParts = "/street/house/listings";
  ...
})
.use(function(req, res) {
  additionalLogic(req.staticParts);
});

This avoids string operations and is very explicit, therefore very robust.

Parsing the URL into staticParts without knowledge of the routes is problematic. For example, parsing the URL /street/house/25 based on keywords would lead to req.staticParts = "/street/house", but there is no matching route for it.

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