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

08: Mojito v0.9: Examples of app.js

Joe Catera edited this page Mar 1, 2014 · 4 revisions

This is a quick reference page for finding templates of app.js. If you are trying to convert an older Mojito application so that it can be run with Mojito v0.9, we recommend you read Mojito v0.9: Converting and Starting Applications.

Basic app.js

/*jslint node:true*/

'use strict';

var debug = require('debug')('app'),
    express = require('express'),
    libmojito = require('mojito'),
    app;

app = express();
app.set('port', process.env.PORT || 8666);
libmojito.extend(app);

app.use(libmojito.middleware());
app.mojito.attachRoutes();

app.get('/status', function (req, res) {
    res.send('200 OK');
});

app.listen(app.get('port'), function () {
    debug('Server listening on port ' + app.get('port') + ' ' +
               'in ' + app.get('env') + ' mode');
});

Routing Examples

See Mojito v0.9: Configuring Routing for a collection of examples.

Middleware Examples

See Mojito v0.9: Using Middleware for examples.

Setting Base Contexts

/*jslint node:true*/

'use strict';

var debug = require('debug')('app'),
    express = require('express'),
    libmojito = require('mojito'),
    app;

app = express();
app.set('port', process.env.PORT || 8666);

// Extend the app and pass a context object to set the base context.
libmojito.extend(app, {
    context: {
        runtime: 'server',
        environment: 'staging',
        colo: 'sp1'
    }
});

app.use(libmojito.middleware());
app.mojito.attachRoutes();

app.get('/status', function (req, res) {
    res.send('200 OK');
});

app.listen(app.get('port'), function () {
    debug('Server listening on port ' + app.get('port') + ' ' +
               'in ' + app.get('env') + ' mode');
});
Clone this wiki locally