Maintainable routing for Express.

Solution of the problem with “express-routescan” node module.

Alexander Bykhov
2 min readSep 21, 2013

I think everyone who works with Express and Node, knows something about express application routing and how many lines of code it can take. When you write a test server with few routes it seems OK. But what about maintainable routing in complex web applications?

Of course, you can define callback functions in other files, require the ones and use them like this:

var express = require(‘express’);
var app = express();
var getFn = require(‘./routes/myThread’).getFn;
var postFn = require(‘./routes/myThread’).postFn;
app.get(‘/forum/:fid/thread/:tid’, getFn);
app.post(‘/forum/:fid/thread/:tid’, postFn);
// and so on

But it’s not the best way to solve this problem. I wrote a node module that helps me (and, I hope, may help you) to organise maintainable routing for Express application.

Concept of express-routescan module

In a few words, you create a folder in your project (it can be also a folder with subfolders structure) with files that contains route configuration (route, methods, callback function, middleware functions) like this one:

‘use strict’;module.exports = {
‘/’: function(req, res){
res.send(“It’s main page of application.”);
}
};

Also you can set an array of HTTP methods for one route, an array of middleware functions, use RegExp as route path, define several routesinside one file.

When routes are ready just run express-routescan with your express application as first argument.

var express = require(‘express’);
var routescan = require(‘express-routescan’);
var app = express();routescan(app);

By default express-routescan module works with ./routes/ folder of a project and .js files, but you can reconfigure this and some other options. For more information read the project homepage.

Thank you.

--

--