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

151
Visualizações
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.

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

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.

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