express regular expression string notation

I am new to Javascript and regular expressions, so I was kind of stuck on how to make a route match all urls that begin with /user/....

I could just do app.get(/user/, function(req,req){ /*stuff*/});

But I was wondering how I could do it if I used a string in place of the regex object. For example

app.get("/user/:id", function(req,req){ /*stuff*/});  

Only matches urls with "user" and one parameter. How would I code it so it matched "user" and N parameters

And also whats the difference between using the string or literal javascript regex object? I have found that even in the string notation I can do something like this...

app.get("/user/:d([a-z]*)", function (req, res) {

    //more stuff
});

I'm not sure of a way to pass a single route N parameters. You can however do a few different routes like:

var userController = requrie('./user_controller'),
  isAuthenticated = require('./middleware/is_authenticated');

app.get("/user/:id/address/:addressId", userController.addressById);
app.get("/user/:id/address", userController.address);
app.get("/user/:id", userController.index);

In order to check for something like authentication before executing the controller action you could use middleware for this and place it as the second parameter before the controller action, which would look something like:

app.get("/user/:id", isAuthenticated, userController.index);

In the example above your user_controller.js would looks something like this:

module.exports = {
  index: function(req, res) {
    // index action logic
  },
  address: function(req, res) {
    // address action logic
  },
  addressById: function(req, res) {
    // addressById action logic
  }
};

This would allow you to pass similar but different routes to different controller actions which would give you a little more control over how to sort out the different routes, as that is more of the routers job than the controllers job anyway.

One more thing to note is that the order of the routes matter. It's usually best to put more specific routes from a group of routes first and more generic routes last.

Hope that helps!