×
Community Blog Create an E-Commerce Web App with Node.js Express Framework and MongoDB on Alibaba Cloud

Create an E-Commerce Web App with Node.js Express Framework and MongoDB on Alibaba Cloud

This tutorial explains how to create an e-commerce web app with Node.js Express Framework and MongoDB on Alibaba Cloud.

This article provides a general overview of using Terraform to achieve the goal. If you prefer using a graphical interface, you can refer to the following article:

Starter Guide | Build E-Commerce Web App Using MongoDB and Node.js in One Click


You can access the tutorial artifact, including deployment script (Terraform), related source code, sample data, and instruction guidance from this GitHub project.

Please refer to this link for more tutorials around Alibaba Cloud Database

Overview

This is a demo e-commerce online store web application built with Node.js Express Framework. The backend data store is on MongoDB. In this tutorial, I will explain how to build and deploy on Alibaba Cloud ECS and MongoDB.

These are the key features of this web application:

  • Manage product via an admin account
  • Register and log in with a user
  • Navigate and add products to a shopping cart
  • Place an order

Deployment Architecture:

1

Index

Step 1. Use Terraform to Provision ECS and MongoDB Databases on Alibaba Cloud

Run the Terraform script to initialize the resources. (In this tutorial, we use MongoDB as a backend database, so ECS and MongoDB are included in the Terraform script.) Please specify the necessary information and region to deploy.

2

After the Terraform script execution finishes, the ECS instance information is listed below:

3

  • eip_ecs: The public EIP of the ECS for web app host

For the MongoDB instance information, please go to the Alibaba Cloud MongoDB web console to get the connection URI.

4
5

By default, the username and password are root and N1cetest, which are preset in the Terraform provision script. If you've already changed it, please update it accordingly.

Please replace the string **** with N1cetest in the connection URI string, such as:

mongodb://root:N1cetest@dds-xxxx.mongodb.rds.aliyuncs.com:3717,dds-xxxx.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-55560033

The MongoDB connection URI will be used later when deploying the web application.

Step 2. Deploy and Set up Node.js and Other Basic Components on ECS

Please log on to ECS with ECS EIP. By default, the password is N1cetest, which is preset in the Terraform provision script in Step 1. If you've already changed it, please update it accordingly.

ssh root@<ECS_EIP>

6

Execute the following commands to install Git client, Node.js:

dnf install -y git
dnf install -y npm

Step 3. Build and Deploy the Web Application

Execute the command to check out the project on GitHub.

git clone https://github.com/alibabacloud-howto/solution-mongodb-labs.git

Execute the commands and edit the .env file to set up the MongoDB connection URI (from Step 1) and save the changes to the .env file. By default, I will use 3000 as the web app port. If you'd like to change it, please update it accordingly.

cd ~/solution-mongodb-labs/e-commerce-application
vim .env

7

Execute the following commands to build and install the app:

npm install

8

Now, execute the following commands to start the web app:

cd ~/solution-mongodb-labs/e-commerce-application
npm start

9

Then, open http://<ECS_EIP>:3000/ in a web browser to visit the web app.

10

Usually, we need to run the Node.js app as a daemon process. Now, install pm2 to start or manage the lifecycle of the Node.js web app.

First, enter Ctrl + C to stop the web app process started by executing npm start beforehand. Then, execute the following commands to install pm2 and start the web app via pm2.

cd ~/solution-mongodb-labs/e-commerce-application
npm i -g pm2
pm2 start app.js

11

Then, open http://<ECS_EIP>:3000/ again in a web browser to visit the web app.

Let's do some simple operations on it:

  • Sign Up an admin account for managing product

12

13
14
15

  • Register a normal user and play around

16
17
18

Step 4. Install Mongoku on ECS to Manage Data on MongoDB

Execute the following commands to install the open-source MongoDB Web Admin tool Mongoku on ECS to manage data on MongoDB:

cd ~
npm install -g mongoku
mongoku start --pm2

19

Then, open http://<ECS_EIP>:3100/ again in a web browser to visit the Mongoku Web Admin. Mongoku uses the 3100 port for web apps by default. I've already set this in the security group rule within the Terraform script.

Now, we can add the MongoDB connection URI here as the server to navigate and manage the data for this online store e-commerce web app via Mongoku. Please enjoy.

20
21

0 0 0
Share on

ApsaraDB

451 posts | 96 followers

You may also like

Comments