copyright | lastupdated | ||
---|---|---|---|
|
2017-11-23 |
{:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
This tutorial 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.
- 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
This tutorial uses the following products:
{: #prereqs}
And to develop and run the application locally:
- SDK for Node.js Cloud Foundry App
- Compose for MongoDB database
In this section, you will run a local MongoDB database, clone a MEAN sample code and start the application which will use the local database.
- Install and run MongoDB using the instructions here. Confirm your database is running with the following command.
mongo
If this results in a connection error, confirm that your mongod server is running. 2. In a terminal window, change to a working directory and run the following command to clone the MEAN sample repository.
git clone https://github.com/IBM-Cloud/nodejs-MEAN-stack
cd nodejs-MEAN-stack
- Install the required packages.
npm install
- Copy .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
- Access your application, create a new user and log in
In this section, we will create a Compose for MongoDB database on the cloud using the command line.
- Login to the IBM cloud via the command line and target your IBM Cloud account. More info
bx login
bx target --cf
- Create the instance of Compose for MongoDB. This can also be done using the UI. It is important that you call this service mean-starter-mongodb as the application is configured to look for this service by this name.
bx cf create-service compose-for-mongodb Standard mean-starter-mongodb
Next, you will deploy the node.js application to the cloud. When running in the cloud, the application is configured to look for the "compose-for-mongodb" database service and connect to it.
bx cf push
The source code contains a manifest.yml file which is configured to connect the "mongodb" service to this application. This will allow the application to read the database credentials using the VCAP_SERVICES environment variable.
Once the code been pushed, you should be able to view the app in your 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.
- Adjust the slider to raise or lower the storage allocated to the Compose for MongoDB service.
- 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 application,
Availability Monitoring runs synthetic tests from locations around the world, around the clock 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: