Skip to content

Latest commit

 

History

History
149 lines (102 loc) · 5.76 KB

mean-stack.md

File metadata and controls

149 lines (102 loc) · 5.76 KB
copyright lastupdated
years
2017, 2018
2017-11-23

{:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}

Modern Web Application using MEAN stack

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.

Objectives

  • 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

Architecture diagram

Products

This tutorial uses the following products:

Before you begin

{: #prereqs}

  1. Install Git
  2. Install Bluemix Command Line Tool

And to develop and run the application locally:

  1. Install Node.js and NPM
  2. Install and run MongoDB Community Edition

Apps and Services

  • SDK for Node.js Cloud Foundry App
  • Compose for MongoDB database

Run MEAN application locally

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.

  1. 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
  1. Install the required packages.
   npm install
  1. Copy .env.example file to .env. Edit the contents as needed, at a minimum adding your own SESSION_SECRET.
  2. Run node server.js to start your app
   node server.js
  1. Access your application, create a new user and log in

Create a MongoDB service on the cloud

In this section, we will create a Compose for MongoDB database on the cloud using the command line.

  1. Login to the IBM cloud via the command line and target your IBM Cloud account. More info
   bx login
   bx target --cf
  1. 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

Deploy the Node.js app

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

Live App

Scaling MongoDB Resources

{: #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.

  1. In the application Dashboard, go to Connections -> Click on the MongoDB instance
  2. In the Deployment Details panel, click Scale Resources.
  3. Adjust the slider to raise or lower the storage allocated to the Compose for MongoDB service.
  4. 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 application performance

{: #monitor}

Lets check the health of your application,

  1. In the application dashboard, select Monitoring
  2. Click View All Tests

Availability Monitoring runs synthetic tests from locations around the world, around the clock to proactively detect and fix performance issues before they impact users.

Summary

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

Next steps

Advance to the next tutorial to learn how to: