express-switch
express-switch is a pattern matching middleware for express
Installation
$ npm install express-switch
Parameters
- getter (mandatory) a function that returns the value to match against the pattern
- pattern (mandatory) an object that describes the different routes to follow depending on the value returned by the getter
Getter
The getter is a function that is responsible to compute the value to match against the cases. This function can be synchronous or asynchronous.
Synchronous
The getter is synchronous when the number of parameters is less than 3. It has to return the value.
prototype
{ /* ... */ return value; }
example
var express = ;var eSwitch = ;var app = ; // ... app; // ... app;
Asynchronous
The getter is asynchronous when the number of parameters is more than 2. It has to forward the value to the done callback.
prototype
{ // ... if error ; else ; // ... }
example
var express = ;var eSwitch = ;var app = ; // ... app; // ... app;
Return an Array
If the getter returns/forwards an Array all the values will be analized sequencially, unless one of the middlewares forwards an error.
Pattern
The pattern is an object with the following properties.
- case (mandatory) a lookup table of middlewares or arrays of middlewares.
- default (optional) the default route to follow if none of the cases matches.
prototye
var pattern = case : "value 1" : middleware1 "value 2" : middleware2 middleware3 // ... "value N" : middleware default : middleware // optional