33

Working in Node I need to convert my request path into a relative path so I can drop it into some templates that have a different folder structure.

For example, if I start with the path "/foo/bar" and want the relative path to "/foo", it would be "..", and for "/foo/bar/baz" it would be "../..".

I wrote a pair of functions to do this:

function splitPath(path) {
    return path.split('/').map(dots).slice(2).join('/');
}

function dots() {
    return '..';
}

Not sure if this is the best approach or if it's possible to do it with a regular expression in String.replace somehow?

edit

I should point out this is so I can render everything as static HTML, zip up the whole project, and send it to someone who doesn't have access to a web server. See my first comment.

2
  • Are you able to use root-relative paths? Commented Sep 15, 2012 at 15:35
  • Like starting with a "/"? No, I'm rendering the pages as static html, using fs.write to drop them all into a folder, and then giving that folder to someone so they can see how all the html/css looks. They won't have access to a web server so I want them to be able to just double click a file in Finder and have it work. Root-relative paths won't work with the File system. At least not that I know of. ex: file:///css/foo.css doesn't load. Commented Sep 15, 2012 at 15:40

3 Answers 3

54

If I understand you question correct you can use path.relative(from, to)

Documentation

Example:

var path = require('path');
console.log(path.relative('/foo/bar/baz', '/foo'));
Sign up to request clarification or add additional context in comments.

3 Comments

Hm this is close... but still not quite right. Reread the requirements I posted. If the path is /foo/bar I need '..' When I do path.relative('/foo/bar', '/') I get back '../..' Generally I think this is the right approach though. I might need to study it more.
Marking as the best answer because it is and really I should just revise my folder structure.
If you are doing path.relative('/foo/bar', '/') you should get ../.. because it is two levels up if you just want it to compare to /foo set to(the variable) to /foo(as in the example).
4

Node.js have native method for this purposes: path.relative(from, to).

Comments

0

This might need some tuning but it should work:

function getPathRelation(position, basePath, input) {
    var basePathR = basePath.split("/");
    var inputR = input.split("/");
    var output = "";
    for(c=0; c < inputR.length; c++) {
       if(c < position) continue;
       if(basePathR.length <= c) output = "../" + output;
       if(inputR[c] == basePathR[c]) output += inputR[c] + "/";
    }

    return output;
}

var basePath ="/foo"
var position = 2;
var input = "/foo";
console.log(getPathRelation(position,basePath,input));
var input = "/foo/bar";
console.log(getPathRelation(position,basePath,input));
var input = "/foo/bar/baz";
console.log(getPathRelation(position,basePath,input));

Result:

(an empty string)    
../    
../../

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.