express-route

express-route is a library that let you organize the routes for express applications.

express-route

express-route is a library that let you organize the routes for the express applications.

  • single method API to read and apply the routes
  • ability to read routes synchronously or asynchronously
  • versatile format of the route definition to support all cases

Firstly, you need to define the routes. The route can be a function, an object or an array of objects. They can be in one file or in many files.

The simplest definition may look like this:

module.exports = {
    '/home'function (reqresnext) {
        res.end('Hello!');
    }
};

This definition is simply translated to the following code:

app.get('/', function (reqresnext) {
    res.end('Hello!');
});

get is a default HTTP method. You can change it using an object definition:

module.exports = {
    '/home': {
        methods: ['post'],
        fnfunction (reqresnext) {
            res.end('Hello!');
        }
    }
};

To apply the routes to the express application you need to call route with valid parameters:

var route = require('express-route');
 
// app is a reference to the express application 
// './routes' is a path to the directory with the routes 
// the third parameter is a configuration object 
route(app, './routes', {
    // retrieve routes synchronously 
    sync: true,
 
    // action to invoke when route is marked as restricted: true 
    ensureRestrictionfunction (reqresnext) {
        if (!user.authorized()) {
            res.status(403).end('Forbidden');
            return;
        }
        next();
    }
});

The definition of the route may be slightly more complicated:

'use strict';
 
module.exports = {
 
    // route with restricted setting on 
 
    '/user': {
        fnfunction (reqres) {
            res.end('user');
        },
        restricted: true
    },
 
    // route that accepts several methods 
 
    '/user/categories': {
        fnfunction (reqres) {
            res.end('method ' + req.method + ' post #' + req.params.id);
        },
        methods: ['get', 'post']
    },
 
    // several actions with different HTTP methods for one route 
 
    '/user/posts/:id': [
        {
            fnfunction (reqres) {
                res.end('method ' + req.method + ' post #' + req.params.id);
            },
            restricted: true,
            methods: ['get', 'delete']
        },
        {
            fnfunction (reqres) {
                res.end('method ' + req.method + ' post #' + req.params.id);
            },
            restricted: true,
            methods: ['post']
        }
    ]
};

Apply routes to the express application.

  • app - reference to the express application,
  • path - path to the folder or the file with the routes,
  • settings - a configuration object,
    • sync: Boolean - whether the load of the routings is synchronous or not (optional, true by default),
    • ensureRestriction: Function - middleware which check if the request is authenticated (optional, by default it's a function that accepts all requests),
    • ext: Array - array with valid file extensions, e.g. ['.route'] (optional, ['.js'] by default).