Moving route logic out of app.js

I'm designing an app with node.js and Express, and I was wondering if it was possible to move certain routing logic out of the app.js file. For exapmle, my app.js currently contains:

app.get('/groups',routes.groups);
app.get('/',routes.index);

Is there a way to move this logic out of the app.js file, and only have something like:

app.get('/:url',routes.get);
app.post('/:url",routes.post);

such that all GET requests would be processed by routes.get and all POST requests processed with routes.post?

You could pass a regular expression as the route definition:

app.get(/.+/, someFunction);

This would match anything. However, if you simply want to move your route definitions outside of your main app.js file, it is much clearer to do something like this:

app.js

var app = require('express').createServer();

...

require('routes').addRoutes(app);

routes.js

exports.addRoutes = function(app) {
    app.get('/groups', function(req, res) {
        ...
    });
};

This way, you're still using Express' built-in routing, rather than re-rolling your own (as you'd have to do in your example).

FULL DISCLOSURE: I am the developer of the node module mentioned below.

There is a node module that does kind of what you're asking for (and will, eventually, do more). It offers automatic routing based on convention over configuration for express. The module name is honey-express, but is currently in alpha development and not yet available on NPM (but you can get it from the source at https://github.com/jaylach/honey-express.

A short example of how it works: (Please note that this coffeescript)

# Inside your testController.coffee file. Should live inside /app/controllers
honey = require 'honey-express'

TestController = new honey.Controller
    index: ->
        # @view() is a helper method to automatically render the view for the action you're executing. 
        # As long as a view (with an extension that matches your setup view engine) lives at /app/views/controller/actionName (without method, so index and not getIndex), it will be rendered.
        @view()
    postTest: (data) ->
        # Do something with data

Now, inside your app.js file you just have to setup some simple configuration:

# in your app.configure callback...
honey.config 'app root', __dirname + '/app'
app.use honey.router()

Now anytime a request comes in, honey will automatically look for a controller with the specified route, and then look for a matching action.. for example -

  • /test will automatically route to the index/getIndex() method of testController
  • / will automatically route to the index/getIndex() method of the homeController (the default controller is home), if it exists
  • /test/test will automatically route to the postTest() method of testController if the http method is POST.

As I mentioned, the module is currently in it's alpha state but the automatic routing works wonderfully and has been tested on two different projects now :) But since it's in alpha development, the documentation is missing. Should you decide to go this route, you can look at the sample I have up on the github, look through the code, or reach out to me and I'd be happy to help :)

EDIT: I should also note that honey-express does require the latest (BETA) version of express as it uses features that are not present in 2.x of express.