Timi Ajiboye

Hosting your Sails.js Application - Heroku

Today we're gonna talk about all the things you'll have to do to take the sails.js application you've been developing live.

You need to make sure you have the Heroku CLI installed and set up before continuing.

Heroku Preliminaries

Navigate to your sails.js app root directory and create a new application on heroku by running heroku create in the terminal.

Add-ons

We'll be using the mLab MongoDB add-on as a database and session store. We're also going to use an add-on called Papertrail for log management.

You can add these add ons with your browser via the heroku dashboard or you can run the following commands in your terminal:

heroku addons:create mongolab:sandbox  
heroku addons:create papertrail:choklad  

These will provision the free tiers of the add-ons for your application.

Prepare your Sails app

1. Database

It is expected that you'll want to use separate databases for different environments; one for development and one for production.

Luckily, Sails.js makes it easy to sort this out.

First of all, you have to create connections in api/config/connections.js. This is like a directory of all the databases you'll be connecting to, in different environments.

module.exports = {

    // this is the default connection
    localDiskDb: {
        adapter: 'sails-disk'
    },

    //custom connection for production
    productionMongo: {
        adapter: 'sails-mongo',
        url: process.env.MONGODB_URI
    }
}

There are a couple of things to note about the above.
1. You can name your connections anything you like. I chose "productionMongo" because that's what I wanted to call it.
2. You have to install the sails-mongo adapter using npm or yarn, if you're going to use MongoDB.
3. The url for productionMongo is set to use an environment variable. This particular environment variable is automatically set when you provision the mLab add-on.

Now you have to specify which connection/database which environment should make use of.
For production, go to /config/env/production.js.

module.exports = {  
    models: {
        connection: 'productionMongo'
    }
}

It's pretty obvious that we're specifying that in the production environment, sails should use the productionMongo connection as it's database.

That's not all we have to do concerning the database however. There are a few peculiarities in MongoDB's manner of operation that make the next few steps pertinent.

  • The default migration setting for production will prevent Waterline from creating new indexes at the database layer. This means that any attribute you've set as unique (or just plain created an index for) in your models won't actually enforce uniqueness. The workaround I use for this is in the form of the sails-hook-mongo-auto-index module. You need to install this with npm or yarn. What it does is simple; it will automatically create indexes based on the attributes found in your models.
  • In your /config/models.js, you might want to set schema:true. This is because MongoDB supports schemaless data structures and if set to false, it will allow you to store arbitrary data in a record. If it's set to true, only attributes defined in the model's attributes will be stored.

2. Session

In a production environment, it is necessary to store your sessions somewhere. You can use MongoDB for this or something like Redis. In this example, I'll be using MongoDB.

In /config/env/production.js

    ...
    session: {
        adapter: 'connect-mongo'
        url: process.env.MONGODB_URI
    }

Depending on your version of Node, you have to install a specific version of the connect-mongo adapter with npm or yarn to get it to work.

When using Node version <= 0.12.x, install connect-mongo version 0.8.2. For Node version >= 4.0, install connect-mongo version 1.1.0.

3. Yarn

If you use yarn in development and you want to enjoy faster deployments to production with Heroku, you can switch to the heroku yarn buildpack for Node by running this command in your terminal.

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-nodejs#yarn  

Deploy

Now you can commit your code and deploy to Heroku by pushing to it's remote.

git push heroku master  

Provided you have everything set up correctly and there are no bugs in your application, you'll have successfully deployed your Sails.js application to Heroku.