Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple route loader for express. Enables you to list your routes on a json file instead of adding code to your main application file for each new route.
JavaScript CSS
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
example
src
.gitignore
README.md

README.md

route-loader

Simple route loader for express. Enables you to list your routes on a json file instead of adding code to your main application file for each new route.

Simple Usage

First off we import the package on our app.js file:

var router = require('route-loader');

Then you can change that default routes code generated by express:

app.get('/', routes.index);
app.get('/users', user.list);

To this:

router(app);

Now you can define your routes on a file named "routes.json". It will look like this:

[
    {
        "route": "/",
        "callback": "index.index"
    },
    {
        "route": "/",
        "method": "post",
        "callback": "index.index"
    },
    {
        "route": "/admin",
        "callback": "index.index"
    }
]

The method defaults to "get", but you can change it to any method that express support.

The dot-separated syntax on the "callback" parameter represents the path inside the routes folder to follow in order to find the right callback for that route.

The string "index.about" tells us that the callback is inside a file named "index" and the callback is named "about".

You can also have subfolders, you just need to join it on the route string.

The string "site.index.about" tells us that the callback is inside a folder named site, inside a file named "index" and the callback is named "about".

Advanced Usage

The simple usage examples assumes a few things:

- Your code that answers to the web requests sits on a folder named "routes" (Express default)
- Your routes file is named "routes.json"

If any of the above items is not true you can change the default routing call:

router(app);

To a more advanced one that accepts more parameters:

router({
    app: app,
    routes_directory: 'routes',
    routes_file: 'routes.json'
});

Also, you might want to provide extra data to each of the routes. You can do that on the "routes.json" file. This examples shows how to provide some auth role data so you can roll your authorization system:

[
    {
        "route": "/",
        "callback": "index.index",
        "permission": "all"
    },
    {
        "route": "/",
        "method": "post",
        "callback": "index.index",
        "permission": "all"
    },
    {
        "route": "/admin",
        "callback": "index.index",
        "permisson": "admin"
    }
]
Something went wrong with that request. Please try again.