Routing in express

Making life a little more sane with express-enrouten

From The Couch
Published in
3 min readAug 12, 2016

--

Express.js is an awesome node HTTP framework that really gets things done in a very quick and concise manner.

Express boasts the following as it’s “Hello World”:

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

app.get('/', function (req, res) {
res.send('Hello World!');
});

app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});

That is pretty darn impressive, but things are bound to get real messy as your application grows , and you are going to want to break up your application into multiple modules. Take this index.js for example:

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

app.get('/', function (req, res) {
res.send('Hello World!');
});
app.get('/ping', function (req, res) {
res.send('pong');
});
app.post('/login', function (req, res) {
...
});
app.post('/register', function (req, res) {
...
});
app.get('/task/all', function (req, res) {
...
});
app.post('/task', function (req, res) {
...
});
app.put('/task', function (req, res) {
...
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});

Now one could simply defer one’s logic for each of these routes to separate modules and then register those here, but there is a neat little library that helps make this process easier. Express-enrouten is a library created by the kraken team at PayPal for express route configuration.

Now a question that may come to mind here is that if I use express-enrouten, why don’t I just use Kraken? Well , Kraken is quite opinionated and seems to be geared more towards apps that use server side templating. I generally build apps that are standalone JSON APIs, and while my use case differs, I am still able to take advantage of some of the features of Kraken thanks to the fact that the team has done a great job of separating concerns.

Well , lets get to work separating our own concerns here by bringing in express-enrouten:

npm install --save express-enrouten

Now we can move our logic out into a folder structure like this:

routes
|-index.js
|-login.js
|-register.js
|-ping.js
|-task
|-index.js

Each of these JavaScript files in the routes directory will then register routes in a similar way to this:

// routes/index.js
module.exports = function (router) {
router.get('/', function (req, res) {
res.send('Hello World!');
});
};

Express-enrouten will then look at the file structure of the folder it’s pointed at and determine the route. For example, the code above will be executed at the ‘/’ route. If you had to define that same code in routes/ping.js the route would be ‘/ping’. You can also still define routes like this if you don’t want to make your structure too deep:

// routes/index.js
module.exports = function (router) {
router.get('/', function (req, res) {
res.send('Hello World!');
});
router.get('/ping', function (req, res) {
res.send('Hello World!');
});
};

We could now delete ping.js and still have a ‘/ping’ endpoint. You can read more on how express-enrouten defines routes here.

The last thing we need to do before all this works is simply bring in our middleware and tell it what directory to point to, our index.js now shrinks dramatically as we are no longer using it to define endpoints:

var express = require('express');
var enrouten = require('express-enrouten');
var app = express();
app.use(enrouten({
directory: 'routes'
}));
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});

And that’s it! We now have a much happier looking application, and we shouldn’t have to come back to index.js that often.

--

--

From The Couch

I write about tech and anything else I find interesting