preface

Since the first batch of applets went online in 2017, more and more mobile apps are presented in the form of applets. Small programs have the advantage of being accessible and ready to go, which greatly reduces the burden of users and makes small programs widely spread. In Alibaba, mini programs are also widely used on taobao, Alipay, Dingding, Autonavi and other platforms.

In order to support a large number of applets, the server side faces the following challenges:

  • A large number of small programs are inactive, and the traditional at least one server approach is a waste of resources;
  • During the peak of activity, the call volume of small program increases rapidly, which requires the server to be able to quickly and flexibly scale.

And small program developers tend to be the client/front-end developers, more energy in the development of business code and application quickly online, and no attention to the free service end of the operation and maintenance operations.

Ali Cloud function computing is a fully managed Serverless computing service, which enables developers to build reliable, elastic and secure services without managing servers and other infrastructure, as long as they write and upload code.

Function calculation flexibility, free operation and maintenance, efficient, safe characteristics are very suitable for small program server.

The solution

Function calculation encapsulates a set of applets server template to help applets developers quickly build applets based on function calculation.

Building applets using this template has the following features:

  • High o&M efficiency: Deployment functions can go online without the management server
  • High development efficiency: develop business code directly based on encapsulated data interface
  • Zero-cost startup: the server is based on function calculation, the database is stored in tables, and both are paid by volume and have a large free amount

Workflow of applets

  • Alipay App: it is the carrier of Alipay mini program and runs on the user’s mobile phone
  • Applet client: is the operation page that applet presents to the user
  • Applets server: The logical processing unit of applets, such as user authentication and data access
  • Alipay server: it is the logical processing unit of Alipay App and contains the user’s identity information

Function calculation encapsulates a set of applets server template to help applets developers quickly build applets based on function calculation. The following describes the workflow of the server template.

FC server template workflow

In addition to infrastructure operation and maintenance, the server mainly solves two general problems:

  1. Authentication: How does the API provided by the server authenticate client requests? How can data security be ensured if client information is leaked?
  2. Data access: How does the client perform database and file operations?

This is a common problem encountered by every small program development, and some frameworks or templates can be used to avoid repeated construction. Therefore, we have developed a small program application template based on function calculation, which solves these general problems and enables developers to focus on business logic and quickly develop and launch their own small programs.

  1. The client side small program through alipay App API, authCode, this process will pop up in the App user authorization box;
  2. The applet client initiates the createSession request to the applet server for initial authentication.
  3. The small program server sends the authCode to the Alipay server, and the Alipay server verifies the authCode and returns accessToken.
  4. The applets server generates a sessionId and an STS Token based on the received accessToken, records these two information on the server and returns them to the client. The STS Token is the credential used to access the server function, and the sessionId is the session information established after the previous successful authentication. Through this sessionId, the user information currently requested can be identified.
  5. The applets client needs to obtain data again, and initiates the data acquisition request to the applets server with the sessionId to prove its identity.
  6. The applets server first obtains user identity information according to the sessionId, and then obtains relevant data according to the user information.
  7. Returns user data to the applet client.

Set up process

Register alipay mini program

  • Preparations: Register an Alipay developer account. If you have not registered an Alipay developer account, log in to ant Financial’s open platform with your Alipay account and complete the developer registration. For more information, see the Developer entry notes.
  • Create alipay applet
    • Log in using your Alipay accountAnt Financial open platform into the developer centerCreate a small program, fill in the basic information, and record the APPID
    • To generate the key, you need to configure the key on the small program server and alipay server to verify the transaction data
    • Set the public key in [Settings] -> [Development Settings] – [Interface Checkmark], and record the private key

Build applets server

  • The preparatory work

    • Open function calculation, table storage, log service
  • The deployment of application

    • Go to the console application center and selectMini App Todo List Server TemplateCreate a template for DBInstance/LogProject using the APPID and PrivateKey you just recorded, and create a custom name for DBInstance/LogProject (if the name is Already used, it Already Exists)

Build a small program client

  • The preparatory work

    • Download and install alipay small program developer tools.
  • Building applications

    • In the applets project root directory, install fC-nodejs-sdk:
    npm install git://github.com/aliyun/fc-nodejs-sdk.git#mini-app --save
    Copy the code
    • The TODO LIST client code is inmini-app-todo-list-client
      • Modify the variables defined at the beginning of server.js. These values are automatically printed when the server template is successfully deployed:
information instructions
fcAccountId Ali cloud AccountId
fcRegion The area where functions are deployed, for examplecn-shanghai
fcServiceName Function calculates the service name
sessionFunctionName The name of the session function, for example session
dbFunctionName The name of the db function, for example db

Because fC-nodejs-SDK uses ES6 syntax, when publishing applets, you need to create a file mini-.project. json in the root directory of the applets and fill the following contents:

{
  "node_modules_es6_whitelist": [
     "@alicloud/fc2"]}Copy the code

At this point, the development work is complete, you can debug, online your small program.

conclusion

The stability and elasticity of the back-end service is also a great test with the increase of traffic volume or the sudden increase of access during activities. Functions can be run by calculating the uploaded code, which greatly improves the development efficiency of back-end services. Flexible extensibility in hybrid mode for easy handling of load changes. Server and client templates can quickly build applets based on function calculation, which makes function calculation a good choice for supporting applets.

“Alibaba cloud native technology circle pays attention to micro-service, Serverless, container, Service Mesh and other technical fields, focuses on cloud native popular technology trends, cloud native large-scale practice, and becomes the technology circle that knows most about cloud native developers.”