express-route-loading
Example usage:
myapp
|-> bin
|-> public
|-> routes
|-> index.js
|-> admin.js
|-> admin
|-> index.js
|-> dashboard.js
I will load the routers in this order:
index.js -> admin.js -> admin/index.js -> admin/dashboard.js
In this way, I can put some path base middlewares to a assign path, such as in admin.js :
var auth = require('middlewares/auth');
router.use(auth);
When you use the express-generator package to create a new express.js application, you get a routes folder for free and some boilerplate code. Usually, you have to require all your routes and mount them. That's fine for small apps that only use a few routes, but for large applications this can be hell.
This module is supposed to make this task a little bit easyer by loading all files within a given folder (defaults to ./routes) and mounting them as routes -> router relatively to the ./routes folder.
For example:
myapp
|-> bin
|-> public
|-> routes
|-> index.js
|-> users.js
|-> admin
|-> index.js
|-> dashboard.js
|-> customers.js
This folder structure would generate the following routes:
/
/users
/admin
/admin/dashboard
/admin/customers
Notice that index.js files are translated to root slashes /.
So, the only thing that this module is expecting is a Router (express.Router()) instance to work.
// routes/users.jsvar express = var router = express; // your routes goes here moduleexports = router;
This piece of code wraps the whole thing toggether. The following example is a common "resourcefull" route declaration.
// routes/users.jsvar express = var router = express; router ; moduleexports = router;
The above example would generate something like this:
GET /users
GET /users/:id
POST /users
PATCH /users/:id
DELETE /users/:id
Installation & Usage
npm install express-path-route --save
// app.jsvar express = ;var app = ; // middlewares goes hereapp;// ... // require the module and pass the// express instanceapp; // if you don't use the default routes folder// you can specify the path to your own// as the second argumentapp './path/to/routes'; moduleexports = app;
Credits
This a fork from https://github.com/bigfactory/express-path-route, which is itself a fork from Gabriel Vaquer 's NPM package express-load-routes.