Simplify the MVC/api applications routing.
npm install --save magic-router
All the controllers in the controller folder are loaded and the default routes are configured automatically by the magic-router.
- The default router will be : Hostname/controller(controller filename without extension)/action(method name)
- if an action/method name in controller file have the name of
index
orget
(default index routing) it have default routing asHostname/controller
too. if a controller have multipledefault index routing
names as action/method name in a file this feature will not work. - Default router method used is
all
(app.all()
in express) from version 1.1.5 onwards. (beforeuse
method was used)
From app.js
import express from 'express';
const app = express();
.... .....
import magicRouter from 'magic-router';
//adding all contollers..
// magicRouter.addAll(express_app_instance, options);
magicRouter.addAll(app, { dirPath: './controllers' });
- Usage in example
let options = {
dirPath:'./controllers', // path of controller directory
exclude:[], // optional files path for removing from magic-routering
prefix:'', // global prefix for routing
};
magicRouter.addAll(app, options);
magicRouter.addAll(app, {
dirPath: '../../example/controllers',
// excluding files
exclude: ['../../example/controllers/auth.js'],
});
- Usage in exclude file test
The developers need to focus only on the controllers.
- Controller should be an object.
- If a controller file have multiple modules exported, it choose controller name as module export name. usage example
export default {
// OPTIONAL
// specifying the router is optional for customizing the router path
// default will be action name.
router: {
// route overrides will come here <methodname>:<route>
// a route with specific param will look like the one below
foo1: 'foo/:id',
},
// OPTIONAL
// type is optional for customizing the request type for methods.
// It can be 'get', 'post' or any verb.
// default will be 'all' as in app.all(...
type: {
// type overrides will come here <methodname>:<verb>
foo: 'get',
},
// OPTIONAL
// beforeController is optional for customizing filters or middlewares before request enters
// controller object.
beforeController: [
(req, res, next) => {
console.log('This will be hit before the control is passed to the controller object.');
next();
},
...
// multiple middleware can be configured here in the same way.
// say, authenticate, auditlog etc
],
// OPTIONAL
// beforeAction is optional for customizing filters or middlewares before request enters
// current action corresponding to the route
beforeAction: {
// <methodname>:[middleware1, middleware2, ...]
foo: [print],
},
// foo is an action in this controller
foo(req, res, next) {
res.send('Foo');
},
// foo1 is an action in this controller with a parameter: id
foo1(req, res, next) {
res.send('id :' + req.param('id'));
},
};
Everything except the action methods are optional and you need to write those only if you need to override the default behaviors.
- Get user
- Get user with id
- Get user with invalid id - exception handling
- If a controller file have multiple modules exported, it choose controller name as module export name.
- Adding global prefix for routing in magic-router. Usage example from test
- Automatically/default add router for "index" or "get" action. eg: invoke index method in controller by
`${host}/${controllerName}`
`${host}/${controllerName}/${index}`
- Feature to exclude some controller files from magic-routering.
- Logging the router details.
- All action methods in controller are exception handled, If any exception caused inside an action, Error will forewarded automatically to (next(err);) outside/global (app.js) error hooked methods. The implemented at example.
- Added test for testing the framework.
- Basic structure of routing and it's implementation.
- Feature for adding multiple middileware/methods before route enter into controller. Define the same in beforeController: [method1,method2,..] at controller files/object.
- Feature for adding multiple middileware/methods before route enter into action. Define the same in beforeAction: {actionName1:[method1,method2,..],...} at controller files/object.
- Adding all controller files in an directory for magic-routering.