Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!