-
Notifications
You must be signed in to change notification settings - Fork 215
Mojito Next: Routing
Mojito "Next" is an undetermined future version of Mojito that will have more Express-like features and allow you to choose from a selection of smaller packages for optimization and customization. In this document, we will be focusing on the differences of configuring routing in today's Mojito and Mojito Next.
For most routing configuration in Mojito Next, you can still use routes.json
as you would today. For some use cases, however, you will need to use a different method to accomplish the same thing in Mojito Next. 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 today's Mojito.
In the following sections, we provide examples of how to use routing configurations of today's 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 Next that you can't do now.
[
{
"settings": [ "master" ],
"hello index": {
"verbs": ["get"],
"path": "/",
"call": "hello.index"
}
}
]
var express = require('express'),
mojito = require('mojito'),
app;
app = express();
app.use(mojito.middleware());
app.mojito.attachRoutes();
// setup "/" to map to "hello.index"
app.get('/', mojito.dispatch('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
var ...,
app;
app = express();
app.get('/bar', function (req, res, next) {
req.params.page = 1;
req.params.log_request = true;
next();
}, mojito.dispatch('bar-1.index'));
app.get('/foo', mojito.dispatch('foo-1.index'));
app.get('/*', mojito.dispatch('foo-1.index'));
app.listen(8666);
[
{
"settings": [ "master" ],
"root": {
"verb": ["get"],
"path": "/*",
"call": "foo-1.index",
"params": { "page": 1, "log_request": true }
}
}
]
app.get('/*', function (req, res, next) {
req.params.page = 1;
req.params.log_request = true;
next();
}, mojito.dispatch('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'];
methods.forEach(function (verb) {
app[verb]('/foo/:mojit-action', function (req, res, next) {
var mojitAction = req.params.mojitAction || 'index',
call = '@foo-1.' + mojitAction;
mojito.dispatch(call)(req, res, next);
});
app[verb]('/bar/:mojit-action', function (req, res, next) {
var mojitAction = req.params.mojitAction || 'index',
call = '@bar-1.' + mojitAction;
mojito.dispatch(call)(req, res, next);
});
});
[
{
"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?/, mojito.dispatch('myMojit.index'));
TBD