express-routes-registrar
Description
Adds an MVC structure to a node express app by separating routes into json files and handlers into controllers. Routes are defined as json files that hold the methods and handler names of each route while Handlers are defined in controllers.
Install
npm install express-routes-registrar
Usage
Defining the app routes into json files
/routes/homeRoutes.json
/routes/usersRoutes.json
Exporting routes module
/routes/index.js
moduleexports = homeRoutes : usersRoutes:
Defining handlers into controllers
/controllers/homeController.js
{ // handle / for GET } moduleexports = HomeController;
/controllers/usersController.js
{ // handle /users GET } { // handle /users POST } { // handle /users/:id GET } { // handle /users/:id PUT } { // handle /users/:id DELETE } moduleexports = UsersController;
Exporting controllers module
/controllers/index.js
// create an instance of each controller// can include any factory logic to create controllers const HomeController = ;const UsersController = ; moduleexports = homeController : usersController:
create the registrar
creates a routes registrar object
const routes = ;const controllers = ;const app = ;const routesRegistrar = app;
.register(routes, controllers)
registers all routes and controllers
routesRegistrar;
.registerRoutesJson(routesJson, controller)
registers a routes file and its controller
routesRegistrar;
.registerRouteMethods(route, methods, controller)
registers a route's methods and their controller
const methods = routesusersRoutes'/users';routesRegistrar;
.registerRoute(route, method, handler)
register a route method and its handler
routesRegistrar;
Build
grunt build
License
The MIT License. Full License is here