Overview
mLab provides MongoDB-as-a-Service for cloud-hosted apps. What this means is that MongoDB is hosted on mLab’s servers, not Cloud Foundry’s.
To connect your CF app to a database hosted on mLab, you must first create a service instance on CF, and then bind your app to it.
CF provides a MongoDB URL for you to use to connect your app to the service
I’ve created an instance of the mLab service for our PWS org, UO-CIT. The name of the instance is UO-CIT-mLab.
When you push an app to CF, you need to bind the app to any services it uses. This binding can be handled most simply by using an application manifest. Example: manifest.yaml.
Our book covers the steps to bind web services to an app using CF’s environment variables on pp. 241-246 of Ch. 8.
We will streamline and update the process by using a Node.js module named cfenv, created by Cloud Foundry.
We will use the cfenv API to get the MongoDB URL from Cloud Foundry, and then use the URL to connect our app to mLab’s MongoDB servers.
Setup
The following steps assume you are deploying a ToDo app, and the app’s home directory is ~/Documents/repos/281/projects/Chapter7/app/ToDo/.
The same ideas apply to any app.
- Complete your MongoDB-powered ToDo app for Project 5. It should run locally w/o error.
- Use npm to install the cfenv module in your ToDo directory.
npm install --save cfenv
- Download manifest.yaml from Github to the project folder for your app.
manifest.yaml is an application manifest that the cf push command uses to bind a web service to your app. It also sets the memory size, and names the app. In this case the web service we are using is mLab (formerly MongoLab), which is MongoDB-as-a-Service.
YAML started out as Yet Another Markup Language, but now it ain’t.
- In Sublime, open your ToDo folder, and edit manifest.yaml:
(A) name field: Replace yourDuckIDuserName with your actual DuckID user name.
name:
susanQ-todosApp
(B) command field: Verify that the command line shows how to run your server:
command: node todos-server.js
(C) services field: Replace yourDuckIDuserName with your actual DuckID user name.
services: - susanQ-mLab
- In Sublime, edit todos-server.js to run locally or on a PaaS provider by editing this line:
//See p. 235, ch. 8, in our textbook port = process.env.PORT || 3000;
- Edit todo-server.js to require the cfenv module:
var cfenv = require("cfenv");
- Find these two lines in todos-server.js, and comment them out as shown:
//var db = 'mongodb://localhost/example' //mongoose.connect(db);
- These lines of code should immediately follow the two lines you just commented out.
- Test the app on your virtual machine. After making all the above changes, the app should run locally the same as before.
Deploy the App to Cloud Foundry
- Create a Service Instance using the PWS Apps Manager.
- Run the cf login command to connect to the UO-CIT org.
- Edit package.son in Sublime.
Use the same app name as you did in manifest.yaml:
Example: susanQ-todoApp.
- Create a .cfignore file in the ToDo directory.
By default, when you push an application to CF, all files in the application’s project directory tree are uploaded to your Cloud Foundry instance.
If the application directory contains other files or subdirectories that are not required to build and run your application, the best practice is to exclude them using a .cfignore file.
- The cf push command is very simple when you have an application manifest:
cf push
- Once your app is deployed to CF successfully, test it in Chrome:
http://yourDuckIDuserName-todoApp.cfapps.io/
Example:
susanQ-todoApp.cfapps.io
- For Security, Stop your App, and don’t leave it running.
Although MongoDB is not subject to SQL-injection attacks (which you will learn about in 381), it is subject to No-SQL-injection attacks.
There are two ways to stop/restart an app:
ii) Use the PWS dashboard, as shown here:
Learn how to stop/restart an app one way and/or the other.
Do not leave an MongoDB app running.
CF Orgs
An org in Cloud Foundry is a development account that multiple collaborators can own and use. All collaborators access an org with user accounts. Collaborators in an org share service instances created by the org admin.