javascript - Converting an absolute path to a relative path -
working in node need convert request path relative path can drop templates have different folder structure.
basically if start path "/foo/bar" need relative path ".." if it's "/foo/bar/baz" need "../.."
i wrote pair of functions this:
function splitpath(path) { return path.split('/').map(dots).slice(2).join('/'); } function dots() { return '..'; }
not sure if best approach or if it's possible regular expression in string.replace somehow?
edit
i should point out can render static html, zip whole project, , send doesn't have access web server. see first comment.
if understand question correct can use path.relative(from, to)
example:
var path = require('path'); console.log(path.relative('/foo/bar/baz', '/foo'));
Comments
Post a Comment