router
Router plugin for mixdown
Install
npm install mixdown-router
Usage
HTTP server side router for mixdown. This plugin consumes declarative route table configuration and generates routes.
Mixdown configuration file. There are 2 parts to declare.
-
Params is a hash of named parameters with regex matching. There can only be one match group in the regex. The matched value (the part in the parentheses) is the only part that will be passed to the handler. See the bark param below.
-
Routes hash contains format strings for the routes. These are the same as pipeline-router.
"router": {
"module": "/tests/dogrouter.js",
"options": {
"params": {
"gender": "(\\w+)",
"age": "(\\d+)",
"id": "(\\d{1})",
"bark": "bark-(loud|quiet)"
},
"routes": {
"search": {
"method": "GET",
"path": "/dogs/:gender/:bark/:age",
"handler": "dogs"
},
"single": {
"method": "GET",
"path": "/dog/:id",
"query": [ "hidePictures" ],
"handler": "dog"
},
"create": {
"method": "POST",
"path": "/create/dog/:id",
"body": [ "gender", "age", "phone" ],
"handler": "create"
}
}
}
}
Next, you need to declare a router with handlers. This is accomplished by inheriting from the base router. Example of the dogrouter which is declared above.
var _ = require('lodash');
var util = require('util');
var Router = require('mixdown-router');
var dogList = [
{ id: 1, breed: 'boxer', gender: 'male', bark: 'loud', age: 2 },
{ id: 2, breed: 'boston terrier', gender: 'female', bark: 'quiet', age: 4 },
{ id: 3, breed: 'chihuahua', gender: 'male', bark: 'loud', age: 6 },
{ id: 4, breed: 'poodle', gender: 'female', bark: 'quiet', age: 5 }
];
var searchDogs = function(age, gender, bark) {
return _.filter(dogList, function(d) {
return (d.age == age || !age) &&
(d.gender == gender || !gender) &&
(d.bark == bark || !bark);
});
};
var DogRouter = function() {
Router.apply(this, arguments);
this.index = function() {
var app = this.app;
var req = this.req;
var res = this.res;
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome to ' + app.id + ' search');
};
this.dogs = function(restParams) {
var req = this.req;
var res = this.res;
var results = searchDogs(restParams.age, restParams.gender, restParams.bark);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(results));
};
};
util.inherits(DogRouter, Router);
DogRouter.prototype.dog = function(restParams) {
var req = this.req;
var res = this.res;
var results = _.find(dogList, function(d) { return d.id == restParams.id; });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(results));
};
module.exports = DogRouter;
If you have a mixdown server running, then this should be all you need to get started. See the units for more examples.
Url Generation
Since the route table should be the single source for all things routing, then it makes sense that it should be able to generate as well as interpret. (e.g. bi-directional)
To generate a route, here is an example which is disconnected from mixdown config.
var Router = require('../tests/catrouter.js');
var app = {
plugins: new (require('broadway')).App()
};
app.plugins.use(new Router(){
, app: app
, params: {
"gender": "(\\w+)",
"age": "(\\d+)",
"id": "(\\d{1})",
"bark": "bark-(loud|quiet)"
}
, routes: {
"search": {
"method": "GET",
"path": "/dogs/:gender/:bark/:age",
"handler": "dogs"
},
"single": {
"method": "GET",
"path": "/dog/:id",
"query": [ "hidePictures" ],
"handler": "dog"
},
"create": {
"method": "POST",
"path": "/create/dog/:id",
"body": [ "gender", "age", "phone" ],
"handler": "create"
}
}
});
var uri = app.plugins.router.url('search', {
age: 6,
gender: 'female',
bark: 'quiet'
});
console.log(JSON.stringify(uri));
var href = app.plugins.router.format('search', {
age: 6,
gender: 'female',
bark: 'quiet'
});
console.log(href);
var uriSingle = app.plugins.router.url('single', {
id: 1234,
hidePictures: true
});
console.log(JSON.stringify(uriSingle));
var hrefWithQuery = app.plugins.router.format('single', {
id: 1234,
hidePictures: true
});
console.log(hrefWithQuery);