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

Mojito Next: Routing

Joe Catera edited this page Sep 24, 2013 · 6 revisions

Overview

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 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.

Single Route

Today's Mojito

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

Mojito Next

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

Multiple Routes

Today's Mojito

[
    {
        "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 Next

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

Adding Routing Parameters

Today's Mojito

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

Mojito Next

app.get('/*', function (req, res, next) {
    req.params.page = 1;
    req.params.log_request = true;
    next();
}, mojito.dispatch('foo-1.index'));

Using Parameterized Paths to Call a Mojit Action

Today's Mojito

[
    {
        "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 Next

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

Using Regular Expressions to Match Routing Paths

Today's Mojito

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

Mojito Next

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

Routing Available Only in Mojito Next

TBD

Clone this wiki locally