-
Notifications
You must be signed in to change notification settings - Fork 215
05. Mojito v0.9: Configuring Routing
For most routing configuration in Mojito v0.9, you can still use routes.json
as in previous versions. For some use cases, however, you will need to use a different method to accomplish the same in Mojito v0.9. Developers more familiar or who like the Express way of configuring routing can choose to use Express-routing syntax instead of routes.json
and can even use the Express lower-level API to configure routing in a way that can't be done in previous versions of Mojito.
In the following sections, we provide examples on how to use traditional routing configurations of Mojito and how you could configure the same routes using the Express syntax. We'll note the use cases where you can no longer use routes.json
and show examples of routing in Mojito v0.9 that wasn't possible before.
[
{
"settings": [ "master" ],
"hello index": {
"verbs": ["get"],
"path": "/",
"call": "hello.index"
}
}
]
var express = require('express'),
libmojito = require('mojito'),
app;
app = express();
libmojito.extend(app);
app.use(libmojito.middleware());
// `app.mojito.attachRoutes()` is sugar for the following:
//
// In this example, explicitly setup "/" to map to "hello.index"
app.get('/', libmojito.dispatch('hello.index'));
app.map('/', 'hello index');
app.map('/', 'get#hello.index');
app.listen(8666);
[
{
"settings": [ "master" ],
"root": {
"verb": ["get"],
"path": "/*",
"call": "foo-1.index"
},
"foo_default": {
"verb": ["get"],
"path": "/foo",
"call": "foo-1.index"
},
"bar_default": {
"verb": ["get"],
"path": "/bar",
"call": "bar-1.index",
"params": { "page": 1, "log_request": true }
}
}
]
// use same template as above for "require" boilerplate
// Remember that libmojito.dispatch() returns a middleware fn.
app.get('/bar', libmojito.dispatch('bar-1.index', {page: 1, log_request: true}));
app.map('/bar', 'bar_default');
app.map('/bar', 'get#bar-1.index');
// OR the verbose way
app.get('/bar', function (req, res, next) {
req.params.page = 1;
req.params.log_request = true;
next();
}, libmojito.dispatch('bar-1.index'));
app.map('/bar', 'bar_default');
app.map('/bar', 'get#bar-1.index');
app.get('/foo', libmojito.dispatch('foo-1.index'));
app.map('/foo', 'foo_default');
app.map('/foo', 'get#foo-1.index');
app.get('/*', libmojito.dispatch('foo-1.index'));
app.map('/*', 'root');
app.map('/*', 'get#foo-1.index');
app.listen(8666);
[
{
"settings": [ "master" ],
"root": {
"verb": ["get"],
"path": "/*",
"call": "foo-1.index",
"params": { "page": 1, "log_request": true }
}
}
]
libmojito.dispatch('foo-1.index', { page: 1, log_request: true}));
// OR the verbose way
app.get('/*', function (req, res, next) {
req.params.page = 1;
req.params.log_request = true;
next();
}, libmojito.dispatch('foo-1.index'));
app.map('/*', 'root');
app.map('/*', 'get#foo-1.index');
[
{
"settings": [ "master" ],
"_foo_action": {
"verb": ["get", "post", "put"],
"path": "/foo/:mojit_action",
"call": "@foo-1.{mojit_action}"
},
"_bar_action": {
"verb": ["get", "post", "put"],
"path": "/bar/:mojit_action",
"call": "@bar-1.{mojit_action}"
}
}
]
var methods = ['get', 'post', 'put'];
app.map('/foo/:mojit_action', '_foo_action');
app.map('/bar/:mojit_action', '_bar_action');
methods.forEach(function (verb) {
app.[verb]('/foo/:mojit_action', libmojito.dispatch('@foo-1.{mojit_action}'));
app.map('/foo/:mojit_action', verb + '#' + '@foo-1.{mojit_action}');
app.[verb]('/bar/:mojit_action', libmojito.dispatch('@bar-1.{mojit_action}'));
app.map('/bar/:mojit_action', verb + '#' + '@bar-1.{mojit_action}');
});
[
{
"settings": [ "master" ],
"regex_path": {
"verbs": ["get"],
"path": "/:matched_path",
"regex": { "matched_path": "\d{1,2}_[Mm]ojitos?" },
"call": "myMojit.index"
}
}
]
app.get(/\d{1,2}_[Mm]ojitos?/, libmojito.dispatch('myMojit.index'));