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

221
Views
Node, express, process ".." symbol in url

I'm looking to get node (using express for routing) to resolve all occurrences of the ../ symbol in urls before performing any routing on the url.

For example if apache is queried at the url /a/b/../c/d/e/../../f, it will first resolve all the ../ symbols and will finally serve any content located at /a/c/f.

With node+express, however, /a/b/../c/d/e/../../f is taken literally and will not match routing for /a/c/f. My problem is that the following code will not respond to a request like /a/b/../c/d/e/../../f:

app.get('/a/c/f', function(req, res) {
   return res.status(200).json({ msg: 'it worked!' }); 
});

How can I get node+express to resolve ../ symbols in the url?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try adding (begore first app.get(...)) this middleware:

var path = require('path');

// some other code

app.use(function(req, res, next) {
  var parsedPath = path.resolve(req.path);
  var root = req.protocol + '://' + req.get('Host');
  var redir = path.join(root, parsedPath);
  console.log(redir);
  if (parsedPath !== req.path) {
    res.redirect(parsedPath);
  } else {
    next();
  }
});

This should be not needed as express + node should resolve paths as you would expect.

over 4 years ago · Santiago Trujillo Report

0

You can use the path module to join the URL segments:

const path = require("path");

app.use(function(req, res, next) {
    const pathArr = req.url.split("/");
    req.url = path.join.apply(null, pathArr);
    next();
});

Reassigning it to the url will allow any subsequent express matchers to continue processing it without having to take manual control of serving anything.

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!