Skip to content

IBM-Cloud/nodejs-MEAN-stack

Repository files navigation

Modern Web Application using MEAN stack

This is a basic boilerplate for the MEAN stack (MongoDB, Express, AngularJS and Node.js) on IBM Cloud.

This application uses Databases for MongoDB and Code Engine on IBM Cloud.

The code and detailed steps are discussed in the IBM Cloud solution tutorial titled Modern web application using MEAN stack.

Features

  • MVC project structure
  • Create, edit and delete user accounts
  • Authentication with username/password
  • Protected routes that can only be accessed by authenticated users
  • Bootstrap CSS framework
  • HTTPS built-in if deployed to IBM Cloud
  • Mongoose for MongoDB interactions.
  • PassportJS for authentication, with over 300 authentication strategies to pick from.

Application Requirements

Files & Folders

File Description
.env.example Set custom environment variables for your application. This is the proper way to store credentials and other sensitive values.
server.js Main server file that the Node.js runtime uses. It contains all the server logic.
/server Folder for files used by the Node.js server
/server/models/user.model.js Model for storing users in MongoDB
/public Folder for files delivered to users, such as html and css files
/public/js/app.js Angular application for manipulating and rendering data in browser

Application

  • MongoDB stores user account information and persists sessions (so that a server crash does not log out all users.)
  • Express functions Node.js middleware to handle all HTTP requests and routing.
  • Angular handles HTML templating and data manipulation.
  • Node.js is the runtime for the application.

There is also generous commenting throughout the application which helps explain critical parts of the application.

Running locally

  1. Clone or download this repo onto your machine.
  2. Install application requirements if not done so already.
  3. Open application directory in your terminal and run npm install
  4. If you don't have an account, create a free one here.
  5. Login to your account via the command line: ibmcloud login
  6. Target your desired region ibmcloud target -r ca-tor
  7. Create the instance of Databases for MongoDB on IBM Cloud: ibmcloud resource service-instance-create mean-starter-mongodb databases-for-mongodb standard ca-tor
  8. Copy .env.example file to .env. Edir .env to fill it the required values. You can run ibmcloud resource service-key-create mean-starter-mongodb-key --instance-name mean-starter-mongodb to obtain the MONGODB_URL and CERTIFICATE_BASE64. Choose your own SESSION_SECRET.
  9. Run node server.js to start your app
  10. Open a browser to the link provided in the terminal prompt to view your app

Note that the code assumes a secured connection to MongoDB using SSL and a certificate. This way you can run the app locally but connect to IBM Cloud Databases for MongoDB.

An alternative way of running locally is using the provided Dockerfile.

  • Install docker on your machine and build the docker image
     docker build . -t mean-stack:v1.0.0
    
  • Run the app locally
     docker run -p 8080:8080 --env-file .env -ti mean-stack:v1.0.0 
    

Provision with Terraform

The steps of creating the resouces (database, code engine project, secret, application, .env file, ...) described in Modern web application using MEAN stack are captured in the terraform file main.tf.

Getting started with solution tutorials has a description of how to get started with terraform on a workstation.

terraform init
terraform apply

Provision with Schematics

Schematics is a cloud based Infrastructure as Code runner with cloud based state. It leverages the Terraform configuration described in the previous section.

Create a Schematics Workspace from this github repository

The link above should have opened a Schematics workspace in the create dialog with the github repository pre-configured and terraform 1.4 selected. Change the Workspace name, Resource group, and Location as desired. This will be the resource group of the workspace. The resource group of the resources created will be configured in schematics. Click Create.

In the Schematics workspace you can configure the variables before clicking Apply plan to provision the resources. Check out the Jobs tab - scroll to about the end of the Logs to see the endpoint URL that can be used to access the application.

Create private catalog product for a Deployable Architecture

In the IBM Cloud console create a private catalog and then add a product to the catalog:

Click on the Version in the Version list. It will walk through a wizard where the defaults are correct, just click Next in each page till you reach Validate version, and click Validate, to create a schematics workspace and resources.

After validation is complete create/configure or open the Security and Compliance and run a scan over the entire account using the IBM Cloud Security Best Practices version 1.2.0 profile by opening the profile and click Attach in the action menu. Frequency can be set to 30 days since the first scan will be run presently and that will provide the results that are required.

Create a Project

Create a project and then create a configuration in the project using the private catalog DA product version created in the previous step.

Contribute

Please create a pull request with your desired changes.

Troubleshooting

The primary source of debugging information for your app running in the cloud are the logs. To see them, use the Code Engine web interface.

For more detailed information on troubleshooting your application, see the Troubleshooting apps section in the documentation.

License

See LICENSE.MD for license information.