Express Auto Route automatically load routes from a structured routes/ folder
This helper library originally came about after about a year of slowly improving how we structure our NodeJS and ultimately me being fed up of having to continuously type out
app.get('/blah', blah_handler) and wondering where different paths are being constructed.
I also subscribe to the idea of convention over configuration because we developers have enough to think about. Also for something like NodeJS where there are so many people trying it out for the first time and fewer people blogging about best practices, sometimes its better to just follow convention and learn good ways to structure your app and write re-usable code.
npm install express-autoroute
First things first you need to enable the autorouter and pass it your express app.
var autoroute = require'express-autoroute';autorouteappoptions; //where app is an express app;
For express-autoroute to start working you need to have a
Passed as a JS object to the autoroute initialisation funciton
//defaultsautorouteappthrowErrors: falselogger: require'winston' //autoroute requires winston internally if you don't pass an instance to itroutesDir: 'routes'
throwErrors: Boolean will throw all errors found while loading routes
logger: winston instance as a default it will use the internal default winston object. If you wish to use custom settings then pass in an instance defined within your app.
routesDir: String sets the directory to search for autoroute files
routeFile: String allows loading of one file instead of a directory, useful for testing
This is the contents of a file
routes/blacklists.js. Note that the name of the file has nothing to do with the resulting endpoint so you can structure your files however you like.
moduleexportsautoroute =get:'/blacklists' : get_all'/blacklists/:id' : getpost:'/blacklists/:id' : update'/blacklists' : createdelete:'/blacklists/:id' : del;//do something
As you can see the file is exposing a structured object. This object will be used later to decide what endpoints to create for the express application and will ultimately result in the following commands being executed
appget'/blacklists/' get_all;appget'/blacklists/:id' get_all;apppost'/blacklists/:id' update;apppost'/blacklists' create;appdetete'/blacklists/:id' : del;
And thats it! A nicely declarative way to load your routes.
express-autoroute also supports routes being separated into folders. The folder name will be added as a prefix to the express endpoints. For example, if you use the same
blacklists.js file from before but move it to the folder
routes/api/blacklists.js the resulting express commands will look like this
appget'/api/blacklists/' get_all;appget'/api/blacklists/:id' get_all;apppost'/api/blacklists/:id' update;apppost'/api/blacklists' create;appdetete'/api/blacklists/:id' : del;
A nifty feature in express is the ability to have route specific middlewares, and if they are used correctly they can really reduce the amount of code you write and always keep things DRY. express-autoroute supports the use of route middlewares by just adding an array to the autoroute object. Here is a silly little example but at least it explains the idea:
moduleexportsautoroute =get:'/blacklists' : authentication get_all;//use the user object to get somethingvar myBlacklists = dbgetMyBlacklistsrequseridressendmyBlacklists;//get the user object or whateverrequser = dbUser;next;
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.