Last active
December 18, 2015 15:59
-
-
Save imalberto/5808228 to your computer and use it in GitHub Desktop.
lib/router.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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