Skip to content

Instantly share code, notes, and snippets.

@imalberto
Last active December 18, 2015 15:59
Show Gist options
  • Save imalberto/5808228 to your computer and use it in GitHub Desktop.
Save imalberto/5808228 to your computer and use it in GitHub Desktop.
lib/router.js
var express = require('express'),
mojito = require('mojito'),
app;
app = express();
app.use(mojito.middleware());
app.use(app.router);
app.mojito.attachRoutes();
app.get('/foo', mojito.dispatch('foo.index'));
app.get('/foo', mojito.dispatch('@Foo.help'));
// lib/router.js
module.exports = {
/**
* @public
* @method attachRoutes
*/
attachRoutes: function () {
var my = this,
app = this._app,
routes = app.mojito.store.getRoutes();
for (var name in routes) {
if (routes.hasOwnProperty(name)) {
this._setupRoute(name, routes[name])
}
}
},
/**
* @public
* @method dispatch
* @param {String} routeName the name of the route as defined in `routes.json`
* @return {Function} express.middleware
*/
dispatch: function (routeName) {
var name = routeName;
return function (req, res, next) {
};
},
/**
* Given a name and the route configuration, set it up in express
*/
_setupRoute: function (name, route) {
// setup each route here on app.get|post
}
};
// app.js
app.get('/foo', mojito.dispatch('foo.index'));
// should be equivalent to
{
"foo.index": {
"path": "/foo",
"verbs": ["get"],
"call": "foo.index"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment