expressjs routing configuration object?

Can you simply set all routing configurations in a single object? Basically what I'd like to do is have a file called routing.js that exports an object containing all of the routes. Having to use express.get() for every route is mundane.

I actually have a folder that holds all the files for my routes (I split them up by object). I also separate out my controllers into files as well. The route files then look like this:

/**
 * Module dependencies.
 */
var controller = require('../controllers/index');

/**
 * The API root for this object.
 */

var root = '/';

/**
 * Route definitions.
 */
var routes = function (app) {

  // get index
  app.get(root, controller.getIndex);
};

/**
 * Exports.
 */
module.exports = routes;

Then in my app.js file I do this which automatically loads up all of my route files and configures the 500 and 404 routes:

fs.readdir(__dirname + '/routes', function (err, files) {
  if (err) throw err;
  files.forEach( function (file) {
    require('./routes/' + file)(app);
  });

  /**
   * 500 page.
   */
  app.use( function (err, req, res, next) {

    if (err && err.status === 404) {
      return next();
    }

    res.statusCode = 500;
    res.render('404.html', {status: 500, url: req.url, error: ': ' + err.message});
  });

  /**
   * 404 page.
   */
  app.use( function (req, res, next) {
    res.statusCode = 404;
    res.render('404.html', {status: 404, url: req.url});
  });
});

If you don't want to read the files in like this, the import part is this line:

require('./routes/' + file)(app);

Which allows you to use the route that was defined previously.

I do it this way to maintain control over exactly which routes are created. If you want to automate the process you can use https://github.com/visionmedia/express-resource.

This allows you to do:

var express = require('express'), 
    Resource = require('express-resource'),
    app = express.createServer();

app.resource('forums', require('./forum'));

In order to create automatically routes that point to the following functions in ./forum:

GET     /forums              ->  index
GET     /forums/new          ->  new
POST    /forums              ->  create
GET     /forums/:forum       ->  show
GET     /forums/:forum/edit  ->  edit
PUT     /forums/:forum       ->  update
DELETE  /forums/:forum       ->  destroy

If you want something even simpler, you could do this...

var getRoutes = [['/', controller.index], ['/post', controller.showPost]];

for (var i = 0, len= getRoutes.length; i < len; i++) {
   var path = getRoutes[i][0];
   var controller = getRoutes[i][1];
   app.get(path, controller);
}