1.1.5 • Published 6 years ago

magic-router v1.1.5

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

magic-router

Build Status contributions welcome HitCount GitHub license

Simplify the MVC/api applications routing.

Installation

npm install --save magic-router

API Guidelines

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 or get (default index routing) it have default routing as Hostname/controller too. if a controller have multiple default 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. (before use 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' });

Initializing Opitons

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);
Exclude some controller files from magic-routering
  magicRouter.addAll(app, {
    dirPath: '../../example/controllers',
    // excluding files
    exclude: ['../../example/controllers/auth.js'],
  });

The developers need to focus only on the controllers.

How to write a controller

  • 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.

Screenshots

  • Get user

Alt text

  • Get user with id

Alt text

  • Get user with invalid id - exception handling

Alt text

Release version (current v1.1.5)

1.1.3 : Controller Name logic change

  • If a controller file have multiple modules exported, it choose controller name as module export name.

1.1.1 : Add global Prefix for routing.

1.0.9 : Default index router

  • Automatically/default add router for "index" or "get" action. eg: invoke index method in controller by
`${host}/${controllerName}`
`${host}/${controllerName}/${index}`

1.0.8 : Exclude controller files

  • Feature to exclude some controller files from magic-routering.
  • Logging the router details.

1.0.7 : Exception handling

  • 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.

1.0.6 : Initial release

  • 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.
1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago