---
name: solution-tutorials-mean-stack
title: Modern web application using MEAN stack
description: This tutorial walks you through the creation of a web application using the popular MEAN stack. It is composed of a **M**ongoDB, **E**xpress web framework, **A**ngular front end framework and a **N**ode.js runtime. You will learn how to run a MEAN sample app locally, create and use a managed database-as-a-service (DBasS), deploy the app to IBM Cloud and scale both the runtime and database resources.
last-updated: 2025-10-08
---

> ## Documentation Index
> The table of contents for this documentation set is at https://cloud.ibm.com/docs/solution-tutorials?format=markdown
> The index for all IBM Cloud docs is at: https://cloud.ibm.com/docs/llms.txt
> Use these files to discover more information as needed.

# Modern web application using MEAN stack
{: #mean-stack}
{: toc-content-type="tutorial"}
{: toc-services="codeengine, databases-for-mongodb"}
{: toc-completion-time="1h"}
{: toc-use-case="ApplicationModernization"}

This tutorial may incur costs. Use the [Cost Estimator](https://cloud.ibm.com/estimator){: external} 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 **M**ongoDB, **E**xpress web framework, **A**ngular front end framework and a **N**ode.js runtime. You will learn how to run a MEAN sample app locally, create and use a managed database-as-a-service (DBasS), deploy the app to IBM Cloud and scale both the runtime and database resources.
{: shortdesc}

## Objectives
{: #mean-stack-0}

- Create and run a sample Node.js app locally.
- Create a managed Databases for MongoDB instance.
- Deploy the Node.js app to the cloud using IBM Cloud&reg; Code Engine.
- Scale runtime CPU and memory resources.
- Scale database memory and disk resources.

![Architecture diagram](images/solution7/Architecture.svg){: caption="Architecture diagram of the tutorial" caption-side="bottom"}
{: style="text-align: center;"}

1. The user accesses the application using a web browser.
2. The Node.js app running in Code Engine accesses the Databases for MongoDB database to fetch data.

## Before you begin
{: #mean-stack-prereqs}

This tutorial requires:
* IBM Cloud CLI - This CLI tool will enable you to interact with IBM Cloud.
   * code-engine/ce plugin (`code-engine/ce`) for CLI access to Code Engine resources.
   * **Optional** Container Registry plugin (`container-registry`) for managing container images.
* `git` to clone source code repository.
* **Optional**, if you want to test running the app locally, you will need to [install Node.js](https://nodejs.org/){: external}.

You will find instructions to download and install these tools for your operating environment in the [Getting started with tutorials](https://cloud.ibm.com/docs/solution-tutorials?topic=solution-tutorials-tutorials&format=markdown) guide.

To avoid the installation of these tools you can use the [Cloud Shell](https://cloud.ibm.com/shell){: external} from the IBM Cloud console.
{: tip}

This tutorial demonstrates the use of the command line to create the database, IBM Cloud&reg; Code Engine project and application. There are instructions in the companion [GitHub repository](https://github.com/IBM-Cloud/nodejs-MEAN-stack/#provision-with-schematics) to provision with Terraform locally on your workstation or even simpler provision with Schematics in just a few clicks.


## Create an instance of MongoDB database in the cloud
{: #mean-stack-2}
{: step}

In this section, you will create a Databases for MongoDB instance in the cloud. Databases for MongoDB is a database-as-a-service that is easy to configure and provides built-in backups and scaling. You can find many different types of databases in the [IBM Cloud catalog](https://cloud.ibm.com/catalog?category=databases#services). To create a Databases for MongoDB instance follow the steps below.

1. If you are not logged in, use `ibmcloud login` or `ibmcloud login --sso` to log in interactively. Target your preferred IBM Cloud region and resource group. In the example below we will use the `ca-tor (Toronto)` region and the `default` resource group.

   ```sh
   ibmcloud target -r ca-tor -g default
   ```
   {: codeblock}

   You can find more CLI commands in the [General IBM Cloud CLI (ibmcloud) commands](https://cloud.ibm.com/docs/cli?topic=cli-ibmcloud_cli&format=markdown) topic in the documentation.

2. Create an instance of Databases for MongoDB via the [command line](https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-provisioning&interface=cli&format=markdown) or use the [console UI](https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-provisioning&interface=ui&format=markdown). The service name must be named **mean-stack-mongodb** as the application used in this tutorial is configured to look for the service by this name. For `<region>`, you can choose a region that is closer to you, however we will use `ca-tor` in this tutorial.

   ```sh
   ibmcloud resource service-instance-create mean-stack-mongodb databases-for-mongodb standard ca-tor
   ```
   {: codeblock}

3. Wait for the instance to be ready. You can check the provisioning status with the following command:

   ```sh
   ibmcloud resource service-instance mean-stack-mongodb
   ```
   {: codeblock}

4. Once you have verified the service status changed to "create succeeded", you may proceed to create a service key.

   ```sh
   ibmcloud resource service-key-create mean-stack-mongodb-key --instance-name mean-stack-mongodb
   ```
   {: codeblock}

## Run the MEAN app locally
{: #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 IBM Cloud.
{: shortdesc}

1. Clone the MEAN sample code.

   ```sh
   git clone https://github.com/IBM-Cloud/nodejs-MEAN-stack
   cd nodejs-MEAN-stack
   ```
   {: codeblock}

1. Copy `.env.example` file to `.env`.

   ```sh
   cp .env.example .env
   ```
   {: codeblock}

1. In the .env file, add your own SESSION_SECRET. For MONGODB_URL and CERTIFICATE_BASE64, run the below command:

   ```sh
   ibmcloud resource service-key mean-stack-mongodb-key --output json
   ```
   {: codeblock}

   You can find the value required for **MONGODB_URL** under `credentials -> connection -> mongodb -> composed` and the value for **CERTIFICATE_BASE64** under `credentials -> connection -> mongodb -> certificate -> certificate_base64` in the returned JSON output. Make sure the values added to the `.env` do not include any quotation or space.

1. Optional - Install the required packages.

   ```sh
   npm install
   ```
   {: codeblock}

1. Optional - Run the application locally. Access the app using the URL provided in the output, create a new user and log in.

   ```sh
   node server.js
   ```
   {: codeblock}

Using the cloud shell it is possible to open the application by clicking on the **eye** icon in the upper right and clicking the preview on port **8080**.
{: note}

## Deploy app to the cloud
{: #mean-stack-4}
{: step}

[IBM Cloud&reg; Code Engine](https://cloud.ibm.com/docs/codeengine?topic=codeengine-getting-started&format=markdown) is a fully managed, serverless platform that runs your containerized workloads, including web apps, microservices, event-driven functions, or batch jobs. In this section, you will create a Code Engine project and deploy the containerized Node.js app to the project. In the previous section, the source code reads the `.env` that you have locally to obtain the URL and credentials to the MongoDB service. You will create a secret in the project to contain these same keys/values that will be read by the app when it is run.

We've already built a container image for the application and pushed it to the public IBM Cloud Container Registry. You will use this pre-built container image to deploy the application.
{: shortdesc}

1. Create a project in IBM Cloud Code Engine.

   ```sh
   ibmcloud ce project create --name mean-stack
   ```
   {: codeblock}

2. Create a secret in the project that contains the keys/values from the `.env` file you used earlier to run the application locally, this secret will be consumed by the application running in the cloud. For more about secrets, see [Working with secrets](https://cloud.ibm.com/docs/codeengine?topic=codeengine-secret&format=markdown).

   ```sh
   ibmcloud ce secret create --name mean-stack-secrets --from-env-file .env
   ```
   {: codeblock}

3. Create the application based on the public container image that is based on the same source code downloaded from the `https://github.com/IBM-Cloud/nodejs-MEAN-stack` repository. If you are interested in the steps used to create this image, you can review [create-container-image.md](https://github.com/IBM-Cloud/nodejs-MEAN-stack/blob/master/create-container-image.md){: external}.

   ```sh
   ibmcloud ce application create --name mean-stack-application --image icr.io/solution-tutorials/tutorial-mean-stack --env-from-secret mean-stack-secrets
   ```
   {: codeblock}

4. Once the code has been pushed, you should be able to view the app in your browser. A host name has been generated that can looks like: `https://mean-stack.<CE_SUBDOMAIN>.ca-tor.codeengine.appdomain.cloud/`. The `CE_SUBDOMAIN` is a variable that is [injected into your project and its value](https://cloud.ibm.com/docs/codeengine?topic=codeengine-inside-env-vars&format=markdown#inside-env-vars-app) determined during the creation of your project. You can get your application URL from the console dashboard or command line. Once you access the application, it should look like this:

   ![Live App](images/solution7/live-app.png){: caption="Live App" caption-side="bottom"}

## Scaling the compute resources in Code Engine
{: #mean-stack-scalecompute}
{: step}

Code Engine monitors the number of requests in the system and [scales the application](https://cloud.ibm.com/docs/codeengine?topic=codeengine-app-scale&format=markdown) instances up and down in order to meet the load of incoming requests, including any HTTP connections to your application. If your service needs additional compute resources, or you want to reduce the amount of compute resource allocated you can make these changes in your specific application page.
{: shortdesc}

1. Navigate to the Code Engine [Projects page](https://cloud.ibm.com/containers/serverless/projects){: external}.
2. Click on the **mean-stack** project created earlier.
2. Under **Summary**, click on **Applications**.
3. Click on the **mean-stack-application** created earlier.
4. Click on **Configuration** and then **Runtime** to view the current configuration.

   ![Scale Resources](images/solution7/CodeEngine_ScaleResources.png){: caption="Scale Resources" caption-side="bottom"}
5. Click on **Edit and create new revision** to adjust not only the **CPU and memory**, the **Minimum/Maximum number of instances** as well as the **Concurrency**.
6. Click on **Save and create** to trigger the changes. It will indicate that the application is `Deploying` and `Ready` when complete.


## Scaling the database resources in Databases for MongoDB
{: #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}

1. Access the Databases for MongoDB service instance from the [Resource List](https://cloud.ibm.com/resources){: external} under **Databases**.
2. Click on the **Resources** panel.
   ![Scale Resources](images/solution7/MongoDB_ScaleResources.png){: caption="Scale Resources" caption-side="bottom"}
3. Adjust the **slider** to raise or lower the storage allocated to your Databases for MongoDB database service.
4. Click **Scale Deployment** to trigger the rescaling and return to the dashboard overview. It will indicate that the rescaling is in progress.
5. Alternatively configure autoscaling rules to automatically increase the database resources as its usage is increasing.

## Remove resources
{: #mean-stack-6}
{: removeresources}
{: step}

To remove resource, follow these steps:
1. With the command below, delete the project to delete all its components (applications, jobs and so on).
   ```sh
   ibmcloud code-engine project delete --name mean-stack
   ```
   {: pre}

2. Delete the Databases for MongoDB service key.
   ```sh
   ibmcloud resource service-key-delete mean-stack-mongodb-key
   ```
   {: pre}

3. Delete the Databases for MongoDB service.
   ```sh
   ibmcloud resource service-instance-delete mean-stack-mongodb
   ```
   {: pre}

Depending on the resource it might not be deleted immediately, but retained (by default for 7 days). You can reclaim the resource by deleting it permanently or restore it within the retention period. See this document on how to [use resource reclamation](https://cloud.ibm.com/docs/account?topic=account-resource-reclamation&format=markdown).
{: tip}

## Related Content
{: #mean-stack-7}

- [Text analysis with Code Engine](https://cloud.ibm.com/docs/solution-tutorials?topic=solution-tutorials-text-analysis-code-engine&format=markdown)