Lesson 20

Migrating to Production

Migrating the application from your local machine to production servers


Lesson Outline

Migrating to Production

Everything in the application is working as it should be, but it's only going to work when testing on our local development machine because that's where both our server and CouchDB database are running. In order to be able to test it on a device, and make it accessible to everyone in the world, we will need to migrate to a real hosting solution.

Hosting Options

There are a few paths you can go down when migrating from your local development machine to "the cloud". We will be using Heroku to host the server and Cloudant to host our CouchDB database, but I want to talk a little about the various options for hosting CouchDB databases.

You can just download CouchDB yourself and install it on a server you control. However, this will involve a bit of work to congifure both the server and CouchDB.

One of the most popular options is Cloudant which is a Database as a Service (like Firebase), it's available through IBM and they handle the hosting of the database, you just need to worry about using it. One downside of using a Database as a Service is that there is always the concern that you are relying on a service that could make pricing changes or even shut down completely at some point (this has happened with popular DBaaS solutions in the past). This is less of an issue for Cloudant, since a CouchDB database could easily be ported anywhere (you could just replicate your Cloudant database to a CouchDB database hosted somewhere else, and you would be pretty much good to go). I find Cloudant to be the best overall option, even though I find using IBM's dashboards to be a pain (IBM are massive and have a ton of different products, which naturally seems to lead to a less than ideal interface for managing your accounts/services).

The best self hosted option I have come across is Bitnami. It gives you access to the latest version of CouchDB, and it let's you choose from a variety of providers to host on. You can choose from:

  • Amazon Web Services
  • Google Cloud Platform
  • Oracle Cloud Platform
  • Microsoft Azure
  • CenturyLink
  • 1&1 Cloud Platform
  • Open Telekom Cloud

The benefit of using Bitnami is that it is self hosted, but Bitnami deploys everything for you so it all comes mostly configured out of the box. I say "mostly" configured because there is still quite a bit of server configuration you will need to do yourself, especially if you want to set up an SSL certificate (which is generally a good idea to have by default, but critical if your application needs to communicate sensitive information like a password).

This added difficulty in configuration is ultimately why I decided to go with Cloudant as the "official" solution for this module.

Using Cloudant

Let's first get our CouchDB database set up on Cloudant. Basically all we want to do is replicate what we already have on our local CouchDB database onto Cloudant. To get started, you can sign up for the free trial for Cloudant with IBM: IBM Cloudant. This will allow you to use the "Lite" version of the service without requiring a credit card, but you can also sign up for the full version to get free credits if you wish.

What you will be signing up for is actually an IBM Cloud account which has access to IBM's other services as well. Once you are logged into your account you should navigate to the IBM Cloudant section by going to Products > Databases > IBM Cloudant. Now you will be able to select the option to get a free trial. You should eventually come to a dashboard that looks like this:

IBM Cloud Services

You can now configure your database however you like, but generally just keeping the default options is fine. Make sure to select the Lite plan unless you want to organise paying for higher usage.

IMPORTANT: If you want to be able to replicate your local CouchDB database to Cloudant (not required, but it can make the migration easier) make sure to change the Authentication method to IAM and legacy credentials.

Click Create to create your database.

You will then be taken to your Resource list which is an overview of all of our IBM services. You should notice that there is a section called Services and if this is your only IBM service it should have a (1) next to it. If you expand this, you will see your Cloudant database (which might still be in the process of provisioning). Once provisioning has finished just click on the name, e.g. Cloudant-1v to open the database.

In a moment we will launch the dashboard to get into all the juicy CouchDB stuff, but first, we will need to set up some credentials so that SuperLogin can communicate with the database. Click on the tab on the left that says Service Credentials.

Click on the button that says New credential. You can just keep the default name and role of Manager and then click Add. The new credentials will appear on the screen, and you can click View credentials to see them. We are now going to use these credentials to update the server configuration.

Update the Server

Now we have a situation where we want to use two different configurations for our Database. In the development environment we will want to use our local CouchDB database and credentials, and in the production environment we will want to use our Cloudant database and credentials.

To make this situation smoother, we are going to add a bit more configuration to our Node server. First, we are going to add a new environment variable as now the host address will change depending on whether we are in a development or production environment.

Modify your .env file in your server folder to reflect the following:


NOTE: Make sure to replace yourcouchdbadminpassword with your own local CouchDB admin password.

These are the values we will want to use for development, but for our production environment we will want to use a different set of values:


IMPORTANT: Do not update your .env file with these values. This is just to demonstrate the values we need.

Most importantly, Cloudant does not use a username and password for authentication, it uses IAM (Identity and Access Management). This means that rather than using a username and password we will be using the API key that Cloudant provides for us.

What we want to do is have a way to run our server in development mode locally and have it use the development variables stored in the .env file, and when we run the application in production we want it to use values that we will supply directly to the Node process in the server (e.g. we will add the variables directly through Heroku rather than using a .env file).

To achieve this we are going to make an update to our package.json file.

Modify the scripts section of the package.json file in your server folder to reflect the following:

  "scripts": {
    "start": "node index.js",
    "dev": "node -r dotenv/config index.js"

Now if we run npm start our server will be started without using the environment variables defined in the .env file, but if we use npm run dev it will use the local development variables stored in the .env file. This allows us easily work from the same codebase in development and production.

We are also going to need to make a few updates to our index.js file.

Update the index.js file in your server folder to reflect the following:


Thanks for checking out the preview of this lesson!

The full version of this lesson is only available to pro members. If you would like full access to this module and all of the other pro modules on Elite Ionic you can become a pro member (or log in if you are already a member).