Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

05. Mojito v0.9: Configuring Routing

Albert Jimenez edited this page Mar 24, 2014 · 6 revisions

Overview

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.

Single Route

Mojito v0.8.x and Earlier

[
    {
        "settings": [ "master" ],
        "hello index": {
            "verbs": ["get"],
            "path": "/",
            "call": "hello.index"
        }
    }
]

Mojito v0.9

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);

Multiple Routes

Mojito v0.8.x and Earlier

[
    {
        "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 }
        }
    }
]

Mojito v0.9

// 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);

Adding Routing Parameters

Mojito v0.8.x and Earlier

[
    {
        "settings": [ "master" ],
        "root": {
            "verb": ["get"],
            "path": "/*",
            "call": "foo-1.index",
            "params": { "page": 1, "log_request": true }
        }
    }
]

Mojito v0.9

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');

Using Parameterized Paths to Call a Mojit Action

Mojito v0.8.x and Earlier

[
    {
        "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}"
        }
    }
]

Mojito v0.9

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}');
});

Using Regular Expressions to Match Routing Paths

Mojito v0.8.x and Earlier

[
    {
        "settings": [ "master" ],
        "regex_path": {
            "verbs": ["get"],
            "path": "/:matched_path",
            "regex": { "matched_path": "\d{1,2}_[Mm]ojitos?" },
            "call": "myMojit.index"
        }
    }
]

Mojito v0.9

app.get(/\d{1,2}_[Mm]ojitos?/, libmojito.dispatch('myMojit.index'));
Clone this wiki locally