abagshaw
abagshaw

Reputation: 6582

expressjs get sections of url between slashes

If I setup a simple server using express for node.js listening on port 80 as follows:

app.get('/*', function (req, res) {
    //mystuff
}

Imagine I trigger this function by going to http://www.myapp.com/first/second/third

How could I get the sections of the url between the slashes and put them in some format (array of strings). So I can get ["first", "second", "third"]

Thanks!

Upvotes: 0

Views: 171

Answers (2)

Aikon Mogwai
Aikon Mogwai

Reputation: 5225

req.url.split('/')

Some another letters to minimal 30 symbols.

Upvotes: 1

owais
owais

Reputation: 4922

split them with / and split will return an array

app.get('/*', function (req, res) {
var sections = req.url.split('/');
console.log(sections);
}

Upvotes: 2

Related Questions