In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to build a Mini Program based on Serverless architecture. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Preface
In order to support large amounts of Mini Program, the challenges facing the server are:
A large amount of Mini Program is inactive, and the traditional way of at least one server will result in a waste of resources.
During the peak period of activity, the transfer amount of Mini Program surged, which requires the server to be able to scale quickly.
On the other hand, Mini Program developers are often client / front-end developers, who pay more attention to the rapid launch of business code and applications, rather than the operation and maintenance of the server.
Ali Cloud function Computing is a fully managed Serverless computing service that allows developers to build reliable, flexible and secure services by writing and uploading code without managing infrastructure such as servers.
The flexible, maintenance-free, efficient and secure features of the function are very suitable to be used as the server of Mini Program.
Solution
Function computing encapsulates a set of Mini Program server templates to help Mini Program developers quickly build Mini Program based on function computing.
Using this template to build Mini Program applications has the following characteristics:
High efficiency of operation and maintenance: the deployment function can be online without managing the server
High development efficiency: develop business code directly based on encapsulated data interface
Zero-cost startup: the server is calculated based on function, and the database is stored in tables, all of which are paid by quantity and have a large free quota.
The workflow of Mini Program
A complete Alipay Mini Program requires the following elements:
Alipay App: it is the carrier of Alipay Mini Program, which runs on users' mobile phones.
Mini Program client: the operation page shown by Mini Program to the user
Mini Program server: it is the logical processing unit of Mini Program, such as authenticating users and accessing data
Alipay server: it is the logical processing unit of Alipay App, which contains the user's identity information.
Function computing encapsulates a set of Mini Program server templates to help Mini Program developers quickly build Mini Program based on function computing. The workflow of the server template is described below.
FC server template workflow
In addition to the operation and maintenance of infrastructure, the server mainly solves two common problems:
Identity authentication: how does the API provided by the server authenticate the client's request? How to ensure data security if the information of the client is leaked?
Data access: how does the client perform database and file operations?
This is a common problem that every Mini Program development will encounter, and repeated construction can be avoided through some frameworks or templates. So we developed a Mini Program application template based on functional computing to solve these general problems and enable developers to focus on business logic and quickly launch their own Mini Program.
The client Mini Program obtains the authCode through the API of Alipay App. This process will pop up the user authorization box in the App.
The Mini Program client initiates a createSession request to the Mini Program server for initial authentication.
The Mini Program server passes the authCode to the Alipay server, which verifies the authCode and returns accessToken.
The Mini Program server generates a sessionId and a STS token based on the received accessToken, records the two messages on the server, and then returns them to the client. STS token is the credential used to access the server function, and sessionId is the session information established by the previous successful authentication, through which the sessionId can identify the currently requested user information.
The Mini Program client once again needs to obtain data and initiates a request to the Mini Program server to obtain data with the sessionId that proves its identity.
Mini Program server first obtains user identity information according to sessionId, and then obtains relevant data according to user information.
Return user data to the Mini Program client.
Set up a process to register Alipay Mini Program
Preparation: sign up for Ant Financial Services Group developer account if you have not signed up for Alipay developer account, use Alipay account to log in to Alipay open platform and complete developer identity registration. For more information, please see the developer residence instructions.
Create the Alipay Mini Program app
Use the Alipay account to log in to Ant Financial Services Group's open platform and enter the developer Center to create Mini Program, fill in the basic information, and record the APPID.
To generate a key, you need to configure the key on the Mini Program server and the Alipay server to verify the transaction data.
Configure the public key in * * Settings * *-> * * Development Settings * *-> * * API signature method * *, and record the private key
Build Mini Program server
Preparatory work
Activate function calculation, table storage, log service
Deploy the application
Enter the console Tencent App Center, select the Mini App Todo List Server Template template, fill in the APPID and PrivateKey just recorded, give DBInstance/LogProject a custom name (if the name is already occupied, the error of Already Exists will be reported), and deploy the application.
Build Mini Program client
Preparatory work
Download and install the Alipay Mini Program developer tool.
Build application
Npm install git://github.com/aliyun/fc-nodejs-sdk.git#mini-app-save
Modify the variables defined at the beginning of server.js. These values will be automatically output after the server template is successfully deployed:
The TODO LIST client code is in mini-app-todo-list-client
Under the root directory of the Mini Program project, install fc-nodejs-sdk:
The information indicates the region where the fcAccountId Ali Cloud AccountIdfcRegion function is deployed, for example, the cn-shanghaifcServiceName function calculates the service name, the name of the sessionFunctionNamesession function, such as the name of the sessiondbFunctionNamedb function, such as db
Because fc-nodejs-sdk uses ES6 syntax, when publishing Mini Program, you need to create a new file mini.project.json in the root directory of Mini Program project, and enter the following:
{"node_modules_es6_whitelist": ["@ alicloud/fc2"]}
At this point, the development work is complete, you can debug and launch your Mini Program.
The above is how to build Mini Program based on Serverless architecture, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.