AMP for E-Commerce Part 3: Integrating the Entire Application with Alibaba Cloud ApsaraDB for MongoDB

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: In this three-part tutorial, we will explore how to create a fully functional e-commerce mobile application using AMP.

By Sai Sarath Chandra, Alibaba Cloud Tech Share Author and Alibaba Cloud MVP

This blog entry is a continuation of the article AMP for E-Commerce Part 2.

In our previous discussion, we have learned a lot about the AMP Framework, ECS Instances, and Alibaba Cloud ApsaraDB for MongoDB. Now you are familiar with all the products required, we will actually create a solution with all the pieces of all the amazing information available & will actually solve a real world scenario.

Overall Architecture

These are the pieces of solution we will work to achieve the below architecture

1

The above is a very simple architecture where a customer makes a request to the AMP Page via browser and it hits our ECS Instance which is running a webserver hosting the website/ webpages this AMP powered site internally uses the another application we created using Express JS on the node platform which connects to the Alibaba Cloud ApsaraDB for MongoDB makes the read request according to the client request and passes the data back very quickly then the node hosted API will pass the information back to the AMP Pages and then it will be used by the webpage to process and display the relevant information.

1.As we seen in the first tutorial, we have already created the Alibaba Cloud ApsaraDB for MongoDB in a very detailed way.
2.Now we are going to create the Node based API to fetch information from ApsaraDB for MongoDB.
3.We will also modify the Existing Open source Apache 2.0 AMP Template to suit our requirements. The reason we do this because of our precious time completing the whole design in the tutorial doesn’t meet the purpose. But I will go through the important part of the code and explain the same.
4.We will integrate the ApsaraDB for MongoDB with the AMP website to make it truly dynamical and we test the same.

Well, it’s time to get started.

Inserting Data onto the Database

Before we start creating and testing we need to insert some data onto the DB.

Software to be downloaded onto ECS

1.Visual Studio Code (preferably latest version)

  • To edit the code, this is the editor I use, you can switch to any favorite editor of yours

2.Google Chrome

  • We are going to use a plugin in chrome to keep this faster with the demo.

3.Web server plugin for chrome

4.ROBO 3T's - Studio 3T product.

  • This in my opinion is a very good product which provides GUI interactions with the MongoDB without compromising the ability to create and test Queries.

Uploading data onto ApsaraDB for MongoDB

1.Logon to the ECS Instance

2.Open Studio 3T product. You will be seeing something related to below screenshot

2

Click on connect then you will see the following window

3

I have already added the server, but I will show you how to add one, make sure you the "Connection String URI" from the ApsaraDB for MongoDB console. We will be connecting using this string

4

If you click on "New Connection" and selected the "From URI..", from the popup you will see an input box to enter the connection URI string.

Please make sure that you are entering the Connection URI String with your root password replaced at ****. If you do that correctly you will be seeing something related to this.

5

If you see something similar to the above screenshot then congratulations!! You have connected to the database.

Now we insert the data into MongoDB

Navigate to the "ExpressJS based API/json/" in the cloned code. I will show a sample insert into the database using Studio 3T.

For Example, I choose "high-low-accessories-products.json"

{
"name": "Chain set",
"description": "Silver alloy construction for durability.",
"price": 867,
"image": "/img/e-commerce/product/product-3.jpg",
"category": "accessories"
},
{
"name": "Red Cruiser",
"description": "Smooth ride for enjoyable cruising.",
"price": 688,
"image": "/img/e-commerce/product/product-6.jpg",
"category": "accessories"
},
{
"name": "Road Bike",
"description": "Built with lightweight aluminum for speed.",
"price": 644,
"image": "/img/e-commerce/product/product-9.jpg",
"category": "accessories"
},
{
"name": "Fixie Blue",
"description": "Designed to get you there.",
"price": 643,
"image": "/img/e-commerce/product/product-2.jpg",
"category": "accessories"
},
{
"name": "16-Speed",
"description": "Smooth shifting through all gears for city riding.",
"price": 524,
"image": "/img/e-commerce/product/product-5.jpg",
"category": "accessories"
},
{
"name": "Leather Saddle",
"description": "Firm, yet comfortable for long leisurely rides.",
"price": 476,
"image": "/img/e-commerce/product/product-4.jpg",
"category": "accessories"
}

Navigate to Studio 3T, expand the "admin" database and right click on the collections and select "Add Collection"

6

To reiterate collection is a group of information which you will be placing in this case this json structure. You will see the below screen.

7

We will give the name of the "high-low-accessories-products" without the extension .json and click on "Create". Then navigate to the collection

8

Switch to JSON View and copy paste the JSON provided. Just click of for the popups which come, as they basically say that the _id field is populated accordingly and the datatypes conversions also happens automatically. If everything happens successfully then you should see the following screen

9

Please go ahead and upload all the .json documents in the same fashion and you should see 8 collections as in the image shown above.

Creating & Testing Node-based Express API Accessing ApsaraDB for MongoDB

We will work on deploying an API which will interact to Alibaba Cloud's ApsaraDB for MongoDB for fetching the data as serve it to the front end.

For the sake of time, please go to the following link and clone the code

https://github.com/saichandu415/AMP-Ecommerce-Apsara/tree/master/ExpressJS%20based%20API

This is a very simple API, focusses only on the retrieving the data from the database.

Let me explain what the code is and what it does. The whole code is actually in the "app.js"

'use strict'

var express = require('express');
var formidable = require('formidable');
var uuid = require('node-uuid');
var https = require('https');
var fs = require('fs');
var sprintf = require("sprintf-js").sprintf;
var mongoClient = require('mongodb').MongoClient;
var dummyJson = require('./json/dummyvalue.json');

var app = express();


//code to enable https
var sslOptions = {
key: fs.readFileSync('./cert/key.pem'),
cert: fs.readFileSync('./cert/cert.pem'),
passphrase: 'saisarath'
};

//Mongo DB Instance Details
var host1 = "dds-6gj54086e0c157941.mongodb.ap-south-1.rds.aliyuncs.com";
var port1 = 3717;
var host2 = "dds-6gj54086e0c157942.mongodb.ap-south-1.rds.aliyuncs.com";
var port2 = 3717;
var username = "root";
var password = "Mongodb123";
var replSetName = "mgset-1050000641";
var demoDb = "admin";

var url = sprintf("mongodb://%s:%d,%s:%d/%s?replicaSet=%s", host1, port1, host2, port2, demoDb, replSetName);

console.info("url generated:", url);

app.get('/ampdemo/getData', function (req, res) {

//Headers to make AMP Accept
res.setHeader('AMP-Access-Control-Allow-Source-Origin','http://127.0.0.1:8000');
res.setHeader('Access-Control-Allow-Origin','http://127.0.0.1:8000');

console.log(JSON.stringify(req.query));

if (req.query && req.query.selection) {
//call promise and fetch data
var getData = fetchFromMongo(req.query.selection);
getData.then(function (dataFrmDB) {
//create response object
var response = {};
response.items = dataFrmDB;
res.status(200).json(response);
}, function (err) {
res.status(400).json(err);
});
} else {
res.status(400).json({ error: 'Please choose a selection' });
}
});


app.use('/', express.static('static'));

https.createServer(sslOptions, app).listen(443, function () {
console.log('Server for "Advanced Interactivity in AMP" codelab listening on port 443!');
});


function fetchFromMongo(collection) {

var coll2Fetch = collection;

return new Promise(function (resolve, reject) {
// Logic to fetch from the MongoDB
mongoClient.connect(url, function (err, db) {
//if error console error
console.log(err);
if (err) {
// Database not connected : error thrown
console.error("connect err:", err);
reject(err);
} else {
//Database connected successfully, get the DB Object
var adminDb = db.admin();
//Authenticate Database
adminDb.authenticate(username, password, function (err, result) {
if (err) {
console.log("authenticate err:", JSON.stringyfy(err));
reject(err);
} else {
console.log('Authenticated : ', JSON.stringify(result));
// Get the Collection handle.
var collection = db.collection(coll2Fetch);
collection.find({}).toArray(function (err, items) {
if (err) {
console.error('Unable to find data' + JSON.stringify(err));
} else {
console.info('data Fetched from MongoDB');
resolve(items);
}
});
}
});
}
});
});
}

sslOptions - You need to remember that AMP cannot talk on http protocols as it strictly prohibits the use of it. We created the certificate ourselves and included the same so the https will be enabled.

MongoDB Instance details – we update the mongoDB details here to get connected to the db instance through code with all the details of

Port – port number of the Node

Host – Host name of the Node

Username – user name of the ReplicaSet

Password – password assigned to the replica set during deployment

replicasetname – Replicasetname generated after the MongoDB instance deployment

DemoDb - The DB name you want to access to , most probably that would be admin considered to this demo.

Headers :

res.setHeader('AMP-Access-Control-Allow-Source-Origin','http://127.0.0.1:8000');
res.setHeader('Access-Control-Allow-Origin','http://127.0.0.1:8000');

These headers are very important because this says that you are saying to the API to access the cross domain requests. In prduction scenarios, please replace the localhost IP with the actual IP.

FetchFromMongo() :

This method is to fetch the data from MongoDB after several steps. Let's look into the steps to fetch the information from Alibaba Cloud ApsaraDB for MongoDB.

1.Get the query parameter from the request

  • We are eliminating the use of multiple URI's from the API end by using one endpoint and varying the information based on the query parameter

2.Get the mongoClient object

  • We have to get the instance of mongoClient object to perform the basic functionalities like authentication etc..

3.Get the database

  • Once the DB is connected. Point the Database you want to do operations on

4.Authenticate the DB

  • Perform the authentication step and get the full rights to the Database

5.Identify the collection

  • The Collection needs to be identified we would like to do the operation on, we are selecting the collection based on the query parameter

6.Operate and structure the collection Data

  • Once we found the data we need make operations on the data and restructure the data based on requirements

URI format
The URI should be "https://<host>:<port>/ampdemo/getData?selection=<collectionname>>"

Missing the selection parameter will throw the following error

400 Bad Request – Please choose a selection

Use HTTPS
AMP only accepts the https protocol so please make sure that the API you are deploying is exposed over https. This current API will work on the port 443 https enabled

How to run the API

If you are using visual studio then go to editor & press "CTRL + SHIFT + C" this will bring up the command prompt then you need to run command "node app.js"

If you are not using visual studio then you need to open command prompt from start menu & navigate to the corresponding folder using the "cd" command and then run the command "node app.js". In both cases if you done correctly then you should see something like below.

10

Then you can go ahead to browser and start invoking the API. Change the variable ECS_Internet_IP based on your actual IP address.

https://<ECS_Internet_IP>:443/ampdemo/getdata?selection=high-low-accessories-products

Since we are using the self-generated certificates, we should hit at least once in the browser and click on Add exception to accept the certificates in the system. In real world production scenario, you will see this will not be the issue because your certificate will be properly registered.

11

Once you add the security exception you should be able to see the information in your browser.

12

Congratulations!! You have successfully deployed your first API on an Alibaba Cloud ECS Instance.

AMP E-Commerce UI

Since this can be a complete tutorial by itself. Please go ahead and clone the existing repository we will start exploring how it is done and what we will apply the knowledge of the components we learnt so far.

Clone the following repository at the below link

https://github.com/saichandu415/AMP-Ecommerce-Apsara/tree/master/Website

Let me explain the snippets of code.

<div class="commerce-select-wrapper inline-block ">
<label for="price" class="bold caps h6 md-h7">Sort by:</label>
<select name="price" id="price" class="commerce-select h6 md-h7" on="change: AMP.setState({products: {filter: event.value}})">
<option value="high-low">Price: High-Low</option>
<option value="low-high">Price: Low-High</option>
</select>
</div>
</div>
</div>

<amp-list class="mx1 md-mxn1" [src]="'https://149.129.130.26:443/ampdemo/getData?selection=' + products.filter + '-' + products.category + '-products'" src="https://149.129.130.26:443/ampdemo/getData?selection=high-low-all-products" height="1000" width="300" layout="responsive">
<template type="amp-mustache">
<a href="product-details.amp.html" target="_self" class="commerce-listing-product text-decoration-none inline-block col-6 md-col-4 lg-col-3 px1 mb2 md-mb4 relative">
<div class="flex flex-column justify-between">
<div>
<amp-img class="commerce-listing-product-image mb2" src="{{image}}" width="340" height="340" layout="responsive" alt="{{ name }}" noloading=""><div placeholder="" class="commerce-loader"></div></amp-img>
<h2 class="commerce-listing-product-name h6">{{ name }}</h2>
{{ description }}
</div>
<div class="h6 mt1">&#163;{{ price }}</div>
</div>
</a>
</template>
</amp-list>

AMP uses http://basscss.com/ as their common CSS for most of their templates, but.

If you see this line of code in the <select> change: AMP.setState({products: {filter: event.value}}) Which shows the usage of the event listeners in AMP. Here you are listening to the change event and using AMP.setState construct you are assigning the event.value to the products products.filter key and we will use the same further to create adynamic query.

<amp-list class="mx1 md-mxn1" [src]="'https://149.129.130.26:443/ampdemo/getData?selection=' + products.filter + '-' + products.category + '-products'" src="https://149.129.130.26:443/ampdemo/getData?selection=high-low-all-products" height="1000" width="300" layout="responsive">

amp-list :
Fetches content dynamically from a CORS JSON endpoint and renders it using a supplied template.

It invokes the REST based endpoint and dynamically based on the user dropdown selection and assigns it to the amp-list which then generates the whole code based on the template.
<amp-img class="commerce-listing-product-image mb2" src="{{image}}" width="340" height="340" layout="responsive" alt="{{ name }}" noloading=""><div placeholder="" class="commerce-loader"></div></amp-img>

amp-img :
Here we use amp-img which uses the values/paths from the response and renders on to the template

One more thing to note in the previous example is that you need to keep your ECS Instance IP int eh [src] and src tags to make the template working.

You can also host the website using "Webserver for chrome",as it is very simple to use and host the site both locally and publically without any issue.

This is the download link for the chrome plugin

https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb/related?hl=en

Once you open the app in chrome you will be seeing something related to this

13

You need to select the folder you want to serve over the web. Then the webserver automatically restarts and gives an end point where you can test and make progress. This is not ideal for the real-time production scenarios but it makes the prototyping much faster.

Below are some screen shots how the website looks when you hit the endpoint

14

And this is the product listing page

15

Congratulations! You have deployed the application with the defined architecture.

Conclusion

I know it's a lot we went through the tutorials and you may also have some doubts. Please raise your concerns as issues to the github repo; I will do my best to answer them. Please take some time to read through and learn the required components. Going further these are some which help your work to become easier in a lot of ways. Please find the github link below:

https://github.com/saichandu415/AMP-Ecommerce-Apsara

目录
相关文章
|
弹性计算 NoSQL 安全
AMP for E-Commerce Part 2: Creating Backend with Alibaba Cloud ApsaraDB for MongoDB
In this three-part tutorial, we will explore how to create a fully functional e-commerce mobile application using AMP.
1667 0
AMP for E-Commerce Part 2: Creating Backend with Alibaba Cloud ApsaraDB for MongoDB
|
JSON JavaScript NoSQL
Invoice Application Backend Using ApsaraDB for MongoDB
In part three of this three-part tutorial, we will explore how to create the backend of a fully functional invoice application using ApsaraDB for MongoDB.
2849 0
Invoice Application Backend Using ApsaraDB for MongoDB
|
3月前
|
NoSQL MongoDB 数据库
数据库数据恢复—MongoDB数据库数据恢复案例
MongoDB数据库数据恢复环境: 一台操作系统为Windows Server的虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 工作人员在MongoDB服务仍然开启的情况下将MongoDB数据库文件拷贝到其他分区,数据复制完成后将MongoDB数据库原先所在的分区进行了格式化操作。 结果发现拷贝过去的数据无法使用。管理员又将数据拷贝回原始分区,MongoDB服务仍然无法使用,报错“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
3月前
|
缓存 NoSQL Linux
在CentOS 7系统中彻底移除MongoDB数据库的步骤
以上步骤完成后,MongoDB应该会从您的CentOS 7系统中被彻底移除。在执行上述操作前,请确保已经备份好所有重要数据以防丢失。这些步骤操作需要一些基本的Linux系统管理知识,若您对某一步骤不是非常清楚,请先进行必要的学习或咨询专业人士。在执行系统级操作时,推荐在实施前创建系统快照或备份,以便在出现问题时能够恢复到原先的状态。
296 79
|
3月前
|
存储 NoSQL MongoDB
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
187 8
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
|
2月前
|
运维 NoSQL 容灾
告别运维噩梦:手把手教你将自建 MongoDB 平滑迁移至云数据库
程序员为何逃离自建MongoDB?扩容困难、运维复杂、高可用性差成痛点。阿里云MongoDB提供分钟级扩容、自动诊断与高可用保障,助力企业高效运维、降本增效,实现数据库“无感运维”。
|
6月前
|
NoSQL MongoDB 数据库
数据库数据恢复——MongoDB数据库服务无法启动的数据恢复案例
MongoDB数据库数据恢复环境: 一台Windows Server操作系统虚拟机上部署MongoDB数据库。 MongoDB数据库故障: 管理员在未关闭MongoDB服务的情况下拷贝数据库文件。将MongoDB数据库文件拷贝到其他分区后,对MongoDB数据库所在原分区进行了格式化操作。格式化完成后将数据库文件拷回原分区,并重新启动MongoDB服务。发现服务无法启动并报错。
|
7月前
|
存储 NoSQL MongoDB
微服务——MongoDB常用命令1——数据库操作
本节介绍了 MongoDB 中数据库的选择、创建与删除操作。使用 `use 数据库名称` 可选择或创建数据库,若数据库不存在则自动创建。通过 `show dbs` 或 `show databases` 查看所有可访问的数据库,用 `db` 命令查看当前数据库。注意,集合仅在插入数据后才会真正创建。数据库命名需遵循 UTF-8 格式,避免特殊字符,长度不超过 64 字节,且部分名称如 `admin`、`local` 和 `config` 为系统保留。删除数据库可通过 `db.dropDatabase()` 实现,主要用于移除已持久化的数据库。
457 0
|
7月前
|
存储 NoSQL MongoDB
从 MongoDB 到 时序数据库 TDengine,沃太能源实现 18 倍写入性能提升
沃太能源是国内领先储能设备生产厂商,数十万储能终端遍布世界各地。此前使用 MongoDB 存储时序数据,但随着设备测点增加,MongoDB 在存储效率、写入性能、查询性能等方面暴露出短板。经过对比,沃太能源选择了专业时序数据库 TDengine,生产效能显著提升:整体上,数据压缩率超 10 倍、写入性能提升 18 倍,查询在特定场景上也实现了数倍的提升。同时减少了技术架构复杂度,实现了零代码数据接入。本文将对 TDengine 在沃太能源的应用情况进行详解。
331 0
|
8月前
|
存储 NoSQL MongoDB
数据库数据恢复—MongoDB数据库迁移过程中丢失文件的数据恢复案例
某单位一台MongoDB数据库由于业务需求进行了数据迁移,数据库迁移后提示:“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”

推荐镜像

更多