developer

Creating and Using the Mojito Application Bootstrap File: app.js

Overview

As of Mojito 0.9, applications are started with the file app.js that must be directly under the application directory. In this chapter, we’ll cover how to start your applications, and use your app.js to define routing paths, create middleware, set the port and base contexts, and offer a simple set of instructions to convert older applications so that they are compatible with Mojito v0.9.

Basic Template

For most applications, your app.js will look similar and perform the same functions. In summary, these functions are creating an instance of Mojito, Express, attching the routing paths defined in routes.json, use application configurations and Mojito’s built-in middleware.

Below is what can be considered a general use template that should be sufficient for most applications. The inline comments offer a short explanation of the significant lines of the code.

'use strict';

// Create instances for Express and Mojito as well
// as allowing for debugging.
var debug = require('debug')('app'),
    express = require('express'),
    libmojito = require('mojito'),
    app;

// Create an Express application.
app = express();

// Set the port with a default or use the value of the environment variable `PORT`.
app.set('port', process.env.PORT || 8666);

//  Mojito extends the functionality of the Express app, so that it Mojito can dispatch mojits, etc.
libmojito.extend(app);

// Include Mojito's middleware.
app.use(libmojito.middleware());

// Use the routes defined in `routes.json`.
app.libmojito.attachRoutes();

// Define a simple routing path with Express-like syntax for doing a sanity check.
app.get('/status', function (req, res) {
    res.send('200 OK');
});

// Listen for incoming HTTP requests on the port set earlier.
app.listen(app.get('port'), function () {
    debug('Server listening on port ' + app.get('port') + ' ' +
               'in ' + app.get('env') + ' mode');
});
// Export the app for use by other libraries/hosting environments, etc.
module.exports = app;

Starting Applications

To start applications, install your application dependencies and use node directly with app.js:

  1. $ npm install
  2. $ node app.js

Configuring Routing

You configure routing in app.js, much like you would do for Express applications, but in Mojito applications, you are explicitly executing actions of mojit instances when requests are received for a given path. See Routing to learn how to define routes and explicitly dispatch mojit instances in app.js.

Using Middleware

In Mojito v0.9 and later, you have to explicitly include Mojito middleware in app.js with the following code:

// Create instances for Express and Mojito as well
// as allowing for debugging.
var debug = require('debug')('app'),
    express = require('express'),
    libmojito = require('mojito'),
    app;      // Include Mojito's middleware.

// Include the Mojito middleware.
app.use(libmojito.middleware());

You can also select which Mojito middleware to include and write custom middleware. See Middleware for details.

Setting Base Contexts

The base context was set with the Mojito CLI command mojito start --context {base_context} in Mojito versions 0.8.x and earlier. With the remove of the start command, you now set the base context in app.js by passing a context object to libmojito.extend, where libmojito is an instance of Mojito. In the context object, you can then specify the environment, device, runtime, language, etc.

For example, to set the base context as the development environment, you could use the following:

var express = require('express'),
    libmojito = require('mojito'),
    app = express();
libmojito.extend(app, {
    context: {
        runtime: 'server',
        environment: 'development'
    }
});

The request context is set by incoming HTTP request, so nothing changes in Mojito v0.9.

Converting Mojito v0.8 and Earlier Applications

  1. Delete the file server.js.
  2. Create a basic app.js with the Basic Template.
  3. Review the following sections to see if you need to make further changes:
  4. See examples of app.js in the code examples on GitHub.