Is it possible to automate routing in Express?


Is it possible to automate routing in Express, so I don't have to list out all the routes?

For example: going to URL '/users/first_example' should automatically use the "users.first_example" module.

app.get('/users/:name', function(req,res){
return eval('users.'; //failed attempt

There's got to be something I'm missing, and it would make my code look a lot more elegant.

Much appreciated.

Problem courtesy of: Adam


var users = require('./users');//a module of route handler functions
app.get('/users/:name', function(req,res){
  var handler = users[];
  if (typeof handler === 'function') {
    return handler(req, res);
Solution courtesy of: Peter Lyons


You might want to check this earlier answer on stackoverflow -

A little more code but abstracts routing to the next level and also gives you a cleaner main file.

Discussion courtesy of: Mukesh Soni

I have been working on something like this, focused on REST routes. Take a look at

If your routes are RESTful:

var booster = require('booster'), express = require('express'), app = express(), db = require('./myDbSetup');



You just need to wire up the database/persistence connection layer. You can choose to customize the controller routes, or the models, or any part of it, but all optional.

Discussion courtesy of: deitch

This recipe can be found in it's original form on Stack Over Flow.