split url path on slash "/" with express or node url

Viewed 7896

is there any way to split the url path for example "/de/about in two parts using either express or node url, meaning I would like to have path[0] = es path[1] = about

// doing it this way I get path[0] = "d" and so on
var path = url.parse(req.url).pathname;

thank in advance

2 Answers

You can split the url string using the split() method by specifying a delimiter (/ in your case) and it will return an array.

Example:

var str = "/de/about";
var path = str.split("/");
console.log(path); // path = [ '', 'de', 'about' ]
                   // path[1] = 'de', path[2] = 'about'
Related