Chapter 4: Modulus

Modulus

One of the simplest services for deploying a Meteor application to the web is Modulus.io — a service that’s designed to host Node applications and Mongo databases (although we can use an externally hosted database).

Hosting applications with Modulus has a number of advantages:

  • The interface is user-friendly.
  • There’s minimal setup involved.
  • You can scale with ease.

If you’re looking to launch your first Meteor project to the web and don’t want to worry about too much of what’s going on behind the scenes, then Modulus is what I generally recommend.

Getting Started

To begin, register for an account with Modulus. You’ll be provided with a $15 credit, which is enough credit for a month’s worth of free service.

Create a Modulus account

There’s no free plan, but you’re at least not required to enter your credit card details upon sign-up.

Creating a Project

After registration, click the “Create New Project” link:

Project dashboard

Here, the word “project” is synonymous with “application”. For every application you want to deploy to the service, create a new project.

You’ll be given a choice of how much RAM the project will need. By selecting the 396mb option, you’ll receive a month of free hosting based on the $15 credit.

Create a Modulus project

Choose a name for the project, then click the “Make It So” button. You’ll be taken to a page with a range of instructions, but you can ignore these.

Creating a Database

At this point, we have a choice:

We can create a database on Modulus, or we can use a database that’s hosted with Compose. Creating a database on Modulus is cheaper (and easier in the short-term), but I prefer hosting databases using an external service because:

  • The database management features are slick.
  • It’s easier to move the project to a different server.
  • Your database will scale automatically.

To cater for all preferences:

  1. I’ll show you how to create a database on Modulus.
  2. You can skip this step if you’d like to use Compose.

To continue with this step:

  1. Return to the dashboard (my.modulus.io).
  2. Click the “Databases” link in the sidebar.
  3. Click the “Create Database” button.

Define the following settings:

  • Label.
  • Provider and region. (The default value is fine.)
  • Username.
  • Password.

Then click the “Create” button.

Create a database

You’ll be shown two values:

  • Mongo URI
  • Mongo Console

(These are the same types of values that are provided after creating a database on an external service such as Compose)

Copy the URI value into a text file, replacing the “user” part with the database username and the “pass” part with the database password.

It should look something like this:

mongodb://myusername:mypassword@proximus.modulusmongo.net:27017/u6zobUge

In a few minutes, we’ll see how to use this value.

Command Line Tools

Before we continue, we’ll need to install a set of command line tools that Modulus provides to streamline the deployment process.

To install these tools, enter the following command into the command line:

npm install -g modulus

Then tap the “Return” key. (If the command doesn’t work, try the command again with sudo at the front of it.)

Install the command line tools

Once the tools are installed, navigate into the directory of a Meteor project with the cd command. I’ll be using the “Todos” application as an example, which is inside a “Meteor” folder I created:

cd Meteor/todos

Then run the following command:

modulus login

When prompted, enter the username and password for your Modulus account. (To be clear, this is not the username and password for your Modulus database, but for the account itself.)

You should see a “Signed in as user” message:

Modulus login command

Server Setup

We’re about to deploy the project to the web, and this will be similar to how we deploy the application in the future, but since we haven’t deployed this particular application before, there’s a couple of extra steps involved.

For the moment, run the following command:

modulus deploy

Then, when prompted:

  1. Confirm the deployment.
  2. Wait for the files to upload.
  3. Take note of the URL that appears.

URL of the Modulus app

This is the URL of the Meteor application that’s now live on the web.

But if we copy and paste the URL into a browser, we’ll get an error, and that’s because of those couple of extra steps we still have to take.

App not working

To continue:

  1. Return to the main dashboard.
  2. Click on the project we created earlier.
  3. Select the “Administration” option in the sidebar.
  4. Scroll to the “Environment Variables” section.

You should see the following:

Environment Variables

In this section, we want to define:

  1. The location and login details of a Mongo database.
  2. The Root URL of the application itself.

Once these values have been defined, we’ll be able to deploy the application for a second time, and from that point onward, everything will work as expected.

Click on the first empty field — beneath the “NODE_ENV” field — and write:

MONGO_URL

Then, in the field beside this, paste a Mongo URI value. This could be the value from Compose or Modulus — or any other database host. It doesn’t matter. Just make sure you end up with something that resembles:

MONGO_URL

In the next empty field — beneath the “MONGO_URL” field — write:

ROOT_URL

…and in the field beside this, copy and paste the URL of the application that appeared inside the command line (including the “HTTP” part):

MONGO_URL

Then click the “Save” button.

Deployment

Most of what we’ve done so far — creating the project, database, etc — only needs to be done once when setting up a new server. So while the process has been a bit fiddly, deployment from this point onward is easy.

We only have to run a single command:

modulus deploy

It’s the same command as before, but since we’ve defined the environment variables, the application will now work as expected.

Here, we can see my deployment of the “Todos” example application:

The live Modulus application

If you were to make a change to the project’s files, you would simply have to run the deploy command again to push those changes to the world.

Resources