| subcollection | solution-tutorials | ||
|---|---|---|---|
| copyright |
|
||
| lastupdated | 2021-01-21 | ||
| lasttested | 2020-12-22 | ||
| content-type | tutorial | ||
| services | cloud-foundry-public, databases-for-mongodb | ||
| account-plan | paid | ||
| completion-time | 1h |
{:step: data-tutorial-type='step'} {:shortdesc: .shortdesc} {:new_window: target="_blank"} {:codeblock: .codeblock} {:screen: .screen} {:tip: .tip} {:pre: .pre}
{: #mean-stack} {: toc-content-type="tutorial"} {: toc-services="cloud-foundry-public, databases-for-mongodb"} {: toc-completion-time="1h"}
This tutorial may incur costs. Use the Cost Estimator to generate a cost estimate based on your projected usage. {: tip}
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. You will learn how to run a MEAN starter locally, create and use a managed database-as-a-service (DBasS), deploy the app to {{site.data.keyword.cloud_notm}} and scale the database resources. {: shortdesc}
{: #mean-stack-0}
{: #objectives}
- Create and run a starter Node.js app locally.
- Create a managed database-as-a-service (DBasS).
- Deploy the Node.js app to the cloud.
- Scale MongoDB memory and disk resources.
{:#architecture}
- The user accesses the application using a web browser.
- The Node.js app accesses the {{site.data.keyword.databases-for-mongodb}} database to fetch data.
{: #mean-stack-prereqs}
This tutorial requires:
- {{site.data.keyword.cloud_notm}} CLI,
gitto clone source code repository.
You will find instructions to download and install these tools for your operating environment in the Getting started with tutorials guide.
In addition, make sure you install Node.js.
{: #mean-stack-2} {: step}
{: #createdatabase}
In this section, you will create a {{site.data.keyword.databases-for-mongodb}} database in the cloud. {{site.data.keyword.databases-for-mongodb}} is database-as-a-service that usually easier to configure and provides built-in backups and scaling. You can find many different types of databases in the IBM cloud catalog. To create {{site.data.keyword.databases-for-mongodb}} follow the steps below.
{: shortdesc}
- Login to your {{site.data.keyword.cloud_notm}} account via the command line and target your {{site.data.keyword.cloud_notm}} account.
ibmcloud login
ibmcloud target --cf{: codeblock}
You can find more CLI commands here.
- Create an instance of {{site.data.keyword.databases-for-mongodb}}. This can also be done using the console UI. The service name must be named mean-starter-mongodb as the application is configured to look for this service by this name.
ibmcloud cf create-service databases-for-mongodb standard mean-starter-mongodb{: codeblock}
- Wait for the instance to be ready. You can check the provisioning status with the following command:
ibmcloud cf service mean-starter-mongodb{: codeblock}
- Create the service key.
ibmcloud cf create-service-key mean-starter-mongodb "Service credentials-1"{: codeblock}
{: #mean-stack-runapplocally} {: step}
In this section, you will clone a MEAN sample code and run the application locally to test the connection to the MongoDB database running on {{site.data.keyword.cloud_notm}}. {: shortdesc}
- Clone the MEAN starter code.
git clone https://github.com/IBM-Cloud/nodejs-MEAN-stack
cd nodejs-MEAN-stack{: codeblock}
- Install the required packages.
npm install{: codeblock}
- Copy .env.example file to .env.
cp .env.example .env{: codeblock}
- In the .env file, add your own SESSION_SECRET. For MONGODB_URL and CERTIFICATE_BASE64, run the below command
ibmcloud cf service-key mean-starter-mongodb "Service credentials-1"{: codeblock} You can find the URL under mongodb -> composed and certificate_base64 under mongodb -> certificate in the returned JSON output.
- Run node server.js to start your app
node server.js{: codeblock}
- Access your application, create a new user and log in
{: #mean-stack-4} {: step}
{: #deployapp}
In this section, you will deploy the node.js app to the {{site.data.keyword.cloud_notm}} that used the managed MongoDB database. The source code contains a manifest.yml file that been configured to use the "mongodb" service created earlier. The application uses VCAP_SERVICES environment variable to access the MongoDB database credentials. This can be viewed in the server.js file. To check the VCAP_SERVICES, run ibmcloud cf env mean-stack.
{: shortdesc}
- Push code to the cloud.
{: codeblock}
ibmcloud cf push
- Once the code been pushed, you should be able to view the app in your browser. A random host name been generated that can look like:
https://mean-random-name.mybluemix.net. You can get your application URL from the console dashboard or command line.
{: #mean-stack-scaledatabase} {: step}
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. {: shortdesc}
- Using the console dashboard, locate the MongoDB service instance and click until you are in the Service Details.
- Click on the Resources panel.

- Adjust the slider to raise or lower the storage allocated to your {{site.data.keyword.databases-for-mongodb}} database service.
- Click Scale Deployment to trigger the rescaling and return to the dashboard overview. It will indicate that the rescaling is in progress.
- Alternatively configure autoscaling rules to automatically increase the database resources as its usage is increasing.
{: #mean-stack-6} {:removeresources} {: step}
To remove resource, follow these steps:
- Visit the {{site.data.keyword.cloud_notm}} Resource List. Locate your app.
- Delete the {{site.data.keyword.databases-for-mongodb}} service and its Cloud Foundry alias.
- Delete the app.
{: #mean-stack-7}
{: #related}
- Set up source control and continuous delivery.
- Secure web application across multiple locations.
- Create, secure and manage REST APIs.
