0.0.11 • Published 10 years ago

emvici-router v0.0.11

Weekly downloads
3
License
GPL v3
Repository
github
Last release
10 years ago

emvici-router

High Performance express/connect pluggable router middleware

This Router should perform better than express.Router.

captura de ecra 2015-01-8 as 15 13 47

Installation

npm install --save emvici-router

Usage

Configuring middleware

You can simply create a router and plug it as a middleware using .use method.

Connect

var connect = require( 'connect' ),
    router = require( 'emvici-router' )(),
    app = connect();

app.use( router );

Express

var express = require( 'express' ),
    router = require( 'emvici-router' )(),
    app = express();

app.use( route );

Route construction

var router = require( 'emvici-router' )();

router.addRoute({ /* options */ })

Options

type
  • Defaults to: 'stack'
  • Allowed Inputs: 'stack', 'wizard'
url

This will determine, by filtering url, which requests this route will run.

  • Required
  • Allowed Inputs: String; Regexp; Array of Strings ; Array of previous allowed inputs
  • Examples:
    • '/'
    • '/test/lol'
    • /^\/users\/(test|testa)/$
    • [ '/test', '/test/index' ]
stack
  • Required when type is equal to stack
  • Allowed Inputs: Function; Array of previous allowed inputs
  • Examples:
[
    function ( req, res ) {

        return Promise.cast( req )
            .then( doSomethingAsync );
    },

    function ( req, res, next ) {

        doSomethingAsync( req, function ( err ) {
            if ( err ) return next( err );

            // some logic

            next();
        });

    }
]

Structure convention

We recomend you to place a file called ./router.js so you can save current app router and allow Controllers or other modules on your system to load it up and use it accordely:

// app.js
var connect = require( 'connect' );

var app = connect(),
    router = require( './router' );

app.use( router );
// router.js
var router = module.exports = require( 'emvici-router' )({
  /* options */
  throw404: false
});

router.addRoutes( require( './controller/users' ) );
// controller/users.js

var someSemiNeededMiddleware = function () {
  // ...
};

module.exports = [

    {
        url: [ '/users', '/users/index', '/users/list' ]
        method: 'get',
        stack: [
          someSemiNeededMiddleware,
          function ( req, res ) {
              // ...
          }
        ],
    },

    {
        url: [ '/users/add', '/users/create', '/users/register' ]
        method: 'post',
        stack: function ( req, res ) {
            // ...
        },
    }

];
0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago