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

04. Mojito v0.9: Converting and Starting Applications

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

Creating an app.js and Deleting server.js

In Mojito v0.9, you no longer use the command mojito start to start applications. Instead, you will use node with the file app.js to start your applications. In the present version of Mojito, running mojito start uses the file server.js to start your application.

In the following steps, we're going to show you how to convert your existing applications to use app.js, so you can start using Mojito v0.9 and take advantage of its new features.

  1. Delete server.js.

  2. Create a basic app.js with the following:

    var express = require('express'),
        libmojito = require('mojito'),
        app = express();
    
    app.set('port', process.env.PORT || 8666); 
    
    libmojito.extend(app);
    app.use(libmojito.middleware());
    app.mojito.attachRoutes();
    
    app.listen(app.get('port'));
    
    module.exports = app;
  3. If needed, modify your app.js to configure routing in Mojito v0.9.

  4. If you're using base contexts in your application, follow the instructions in Mojito v0.9: Setting Base Contexts.

  5. To add middleware, see Mojito v0.9: Using Middleware

  6. If you would like to see more examples app.js, see Mojito v0.9: Examples of app.js and our code examples on GitHub.

Running the App

  1. $ npm install
  2. $ node app.js
  3. Open the URL http://localhost:8666 to view your application.

Notes About app.js

The following sections annotate some of the code in the app.js that we used in Creating an app.js and Delete server.js

Creating the Server

We're still requiring Mojito, but instead of calling createServer() to have Mojito create an application for us, we're going to create one with Express ourselves in the next step.

server.js

var Mojito = require('mojito');
var app = Mojito.createServer();

app.js

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

Ports and Contexts

We are setting the port in app.js and not with the configuration appPort in application.json. Also, we are setting the context in app.js and not with the command-line tool option --context.

app = express();
app.set('port', 8666);
libmojito.extend(app, { context: { runtime: 'server' } });

Middleware

Mojito has middleware that you need to manually add in the app.js with the following lines:

app.use(libmojito.middleware());

To add your own custom middleware, you simply write Express middleware.

Configure Routing

To use the routes that you have configured in routes.json, you must manually attach the routing paths to the app object by adding the following line:

app.mojito.attachRoutes();

If your routes.json use regular expressions, you will need to use Express-like implementations. See the following pages to learn how to add these features with Express-like routing expressions:

Clone this wiki locally