-
Notifications
You must be signed in to change notification settings - Fork 214
04. Mojito v0.9: Converting and Starting Applications
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.
-
Delete
server.js
. -
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;
-
If needed, modify your
app.js
to configure routing in Mojito v0.9. -
If you're using base contexts in your application, follow the instructions in Mojito v0.9: Setting Base Contexts.
-
To add middleware, see Mojito v0.9: Using Middleware
-
If you would like to see more examples
app.js
, see Mojito v0.9: Examples of app.js and our code examples on GitHub.
$ npm install
$ node app.js
- Open the URL http://localhost:8666 to view your application.
The following sections annotate some of the code in the app.js
that we used in
Creating an app.js and Delete server.js
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;
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' } });
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.
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: