CS Workshop 👻

Supplementary - System Deployment

To deploy to cloud platforms, there a number of PAAS (Platform as a Service) that can host the system for us. Today, we will use Heroku.

Heroku

Let’s register an account on Heroku first (http://www.heroku.com).

Also, install the Heroku toolbelt via command

su xdeveloper

sudo brew install heroku

exit

or you can download the installer here

https://devcenter.heroku.com/articles/heroku-cli#download-and-install

First, you need to login into Heroku from your command shell

heroku login

You’ll need to enter you email address and password.

Enter your Heroku credentials.
Email: [user@email.com](mailto:user@email.com)
Password (typing will be hidden):

Create Heroku application:

heroku create

Next, create Procfile in your Sails.js application’s folder.

cd path/to/sailjs-project
nano Procfile

and add the following to your Procfile.

web: npm start

Add a remote host for your Sails.js application, in your project directory:

heroku git:remote -a [name-of-the-app-on-heroku]

Commit your code to the repository and deploy it:

git add .
git commit -m "Deploy to Heroku"
git push heroku master

You should wait until you get this message:

remote: -----> Launching... done, v3
remote:        [https://name-of-the-app-on-heroku.herokuapp.com/](https://name-of-the-app-on-heroku.herokuapp.com/) deployed to Heroku

Congratulations! Now you can check your application by this URL: https://name-of-the-app-on-heroku.herokuapp.com

Permanent Storage

But your system doesn’t hook with a mongoDB, meaning your data is volatile and may be gone really soon.

Hooking your system with a mongoDB engine via Heroku is free but you have to provide credit card information.

On the Heroku website, locate your sails project under Personal apps. In your project, click Configure Add-ons.

Search MongoDB in the provided search bar and select mLab MongoDB :: Mongodb

You will then be prompted to input credit card information. Choose the last option (verify) from the pop up box.

After you have added the mLab MongoDB add-on, in the terminal, run

heroku config:get MONGODB_URI

You will then obtain an MongoDB url like the following:

MONGODB_URI => [mongodb://heroku_12345678:random_password@ds029017.mLab.com:29017/heroku_12345678](mailto:mongodb://heroku_12345678:random_password@ds029017.mLab.com:29017/heroku_12345678)

Go back to your connections.js, use the MONGODB_URL you obtained to a new adapter, to the url attribute below.

mLab: {
    adapter: 'sails-mongo',
    url    : `copy the MONGODB_URI here`
}

Use this adapter in production.js, (located under config -> env):

models: {
	connection: 'mLab'
},

session: {
	adapter: 'connect-mongo',
	url: `copy the MONGODB_URI here`
}

We also used MongoDB as our session datastore.

Data in MongoDB

During the development, we may have hardcoded the id of both coupons and users. There, we have used the integer for the id. However, in MongoDB, their primary keys are with ObjectID type. Thus, in our bootstrap.js, let’s remove all the provided id and let mongoDB generates for us.

To re-deploy your application, you’ll need to commit your changes and push to Heroku:

cd path/to/sailjs-project
npm install [sails-mongo@0.12.3](mailto:sails-mongo@0.12.3) --save
npm install connect-mongo --save
git add .
git commit -m "Add mLab connection"
git push heroku master

So now, your data is permanent.

mLab also gives us an interface to manipulate our data, as shown below.

You can reach this page from the Heroku interface.

Crashes

Your sails system may crash. To have it restart automatically after each system crashes, we may install the forever module.

npm install forever --save

Also edit the Procfile:

web: ._/node_modules/_.bin/forever -m 5 app.js

Deploy your system to Heroku again. It will run forever!!

Ref: https://vort3x.me/sailsjs-heroku/ and https://devcenter.heroku.com/articles/mongolab

Written with StackEdit.