npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

magic-router

v1.1.5

Published

routing pattern and high productive way for bulding mvc/api projects

Downloads

5

Readme

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.