This solution walks you through the creation of a web application using the popular MEAN stack. It is composed of a Mongo DB, Express web framework, Angular front end framework and a Node.js runtime.
Time to complete: 15 minutes
- Create and run a starter Node.js app locally
- Create a MongoDB database on the IBM cloud
- Deploy the Node.js app to the cloud
- Scale MongoDB Resources
- Monitor application performance
{: #prereqs}
- Install Git
- Install Node.js and NPM
- Install and run MongoDB Community Edition
- Install Cloud Foundry Command Line Tool
- SDK for Node.js Cloud Foundry App
- Compose for MongoDB database
Configure local MongoDB
Open the terminal window and cd
to the bin
directory of your MongoDB installation. You can use this terminal window to run all the commands in this tutorial.
Run mongo
in the terminal to connect to your local MongoDB server.
mongo
If your connection is successful, then your MongoDB database is already running. If not, make sure that your local MongoDB database is started by following the steps at Install MongoDB Community Edition. Often, MongoDB is installed, but you still need to start it by running mongod. When you're done testing your MongoDB database, type Ctrl+C in the terminal. This guide uses a sample MEAN stack (MongoDB, Express, AngularJS and Node.js) application.
Getting Started
- In the terminal window, cd to a working directory. Run the following command to clone the sample repository.
git clone https://github.com/IBM-Bluemix/nodejs-MEAN-stack
This sample repository contains a copy of the MEAN.js repository. 2. Run the following commands to install the required packages.
cd nodejs-MEAN-stack
npm install
- Rename .env.example file to .env. Edit the contents as needed, at a minimum adding your own SESSION_SECRET.
- Run node server.js to start your app
node server.js
In this step we will create a Compose for MongoDB database on the cloud.
- Set your Cloud Foundry CLI tool's API endpoint to the IBM cloud
bx api https://api.ng.bluemix.net
- Login to the IBM cloud via the command line and target your IBM Cloud ORG and SPACE
bx login
bx target -o ORG -s SPACE
Note: ORG is usually your email address and SPACE is the space you created on the cloud. Learn more on ORG and SPACE here. 3. Create the instance of Compose for MongoDB
bx cf create-service compose-for-mongodb Standard mongodb2
There are many ways in which we can deploy the app to the cloud, the simplest is to push the code by using the following command:
bx cf push
By running this command, the local code will be pushed to the IBM Cloud
Note we created an database instance earlier with the name mongodb. Cloud Foundry (on the IBM cloud) will look for the manifest.yml file, then it will find and bind the mongodb database to our application.
Once the code been pushed, you should be able to view the app in your cloud dashboard. After deployment, a random host name is generated and will look something like: https://mean-random-name.mybluemix.net
{: #database}
If your service needs additional storage, or you want to reduce the amount of storage allocated to your service, you can do this by scaling resources.
- In the application dashboard, go to Connections -> Click on the MongoDB instance
- In the Deployment Details panel, click Scale Resources. The Scale Resources page opens.
- Adjust the slider to raise or lower the storage allocated to the Compose for MongoDB service. Move the slider to the left to reduce the amount of storage, or move it to the right to increase the storage.
- Click Scale Deployment to trigger the rescaling and return to the dashboard overview. A 'Scaling initiated' message appears at the top of the page to let you know the rescaling is in progress and the Deployment Details pane also shows the scaling in progress. When the scaling is complete the Deployment Details pane updates to show the current usage and the new value for the available storage.
{: #monitor}
Lets check the health of your multi-region application,
- Application Overview -> View toolchain
- Click Add a Tool
- Choose Availability Monitoring -> Create Integration
- Select Availability Monitoring and click on the name of your app.
- Click View All Tests
Availability Monitoring constantly runs synthetic tests from locations around the world to proactively detect and fix performance issues before they impact users.
In this tutorial, you deployed a MEAN stack application using Compose for MongoDB to the IBM Cloud. We also covered how to scale the database and monitor the application performance.
You learned how to:
- Create and run a basic Node.js app locally
- Create a Compose for MongoDB database on the IBM Cloud
- Deploy the Node.js app to the IBM Cloud
- Scale MongoDB Resources
- Monitor application performance
Advance to the next tutorial to learn how to: