Welcome toTencent Cloud + community, get more Tencent mass technology practice dry goods oh ~

This article was published by Bai Huancheng in cloud + Community column

Recently, small program opened a new capability — “small program · Cloud development”, to help developers quickly build wechat small program back-end services. As a developer of wechat small program, I also tried the cloud development of small program for the first time, and share my own experience in the development process with you.

What is applets cloud development

To put it simply, applets cloud development is a Serverless service, which provides developers with “cloud functions”, “cloud database” and “cloud file storage”, and encapsulates these capabilities into specific interfaces to be called in wx.cloud.xxx.

What is applets cloud development not

First of all, small program cloud development is not PaaS, which is different from BAE and Google App Engine that we are familiar with. The cloud function provided by it is not a complete environment, but a specific event as a unit. Strictly speaking, the cloud Function it provides is actually Function as a Service, Products in this category include LeanCloud Cloud Functions, Bmob Cloud Functions, AWS Lambda, Azure Functions, and Google Cloud Functions.

Second, applets cloud development is not a BaaS like LeanCloud or Bmob. Different from the above two products, they provide a rich API interface that can realize a variety of functions. Applets · Cloud development encapsulates the interface in depth and can only be called in applets and cloud functions through Wx. cloud and WX-Server-SDK (as of August 24, 2018). Therefore, applets cloud development can only be applied in applets, not in other products (such as apps).

What scenarios does applets cloud development fit into?

All business logic only needs to be completed in the small program side, without too complex management logic (this is because cloud functions and cloud databases cannot be called outside the small program area, so powerful Web management interface cannot be realized)

Advantages of applets cloud development

1. Wechat login logic is simple

Small program cloud development can automatically verify user login. Without verifying user identity again, developers can directly obtain user information through the cloud function event.userinfo. openId. When the database or file storage API is called directly, the openId corresponding to the user is automatically associated

If a user authorizes the applets to obtain information such as nicknames, this information will also automatically appear in the user login section of the admin console for applets cloud development. No manual upload required by the developer.

2. The free

Currently, applets cloud development offers free 1GB of database storage and free 5GB of file storage. This storage is not very large, but it is more than enough for some individual developers to quickly develop their own applets.

3. Simple

The invocation of small program cloud development is very simple, you only need to understand JavaScript and some simple asynchronous knowledge (promise), you can complete the content of small program cloud development.

4. No invasion

Applets cloud development itself encapsulates the basic library level of applets, and you don’t need to introduce other libraries to use it.

Similarly, you can migrate some functionality from your existing application to the applets cloud.

Small program development process encountered some pits

1. Asynchronous requests need to be handled through Promises

In cloud functions, we mostly implement some request interfaces that cannot be implemented in small programs or are limited by domain names. In this case, we cannot use the traditional Callback method to request, because after the traditional Callback method is executed, the cloud function has already returned the data to the client. We need to use promises to handle this.

For example, the following code is my request for the Douban API.

var rp = require('request-promise')

exports.main = (event, context) => {
  var res = rp('https://api.douban.com/v2/book/isbn/'+ event.isbn).then( html => {
    return html;
  }).catch( err => {
    console.log(err);
  })
  return res
}
Copy the code

The code above comes from the Github project WxCloud-BookCase

2. The permission structure is simple

The database permissions provided by applets cloud development are very simple, with only four.

  • Writable by creator only, readable by all: Data writable by creator only, readable by all; Like articles.
  • Creator only: Data can be read and written by the creator only, and other users cannot read and write data. Like using a private photo album.
  • Only the management terminal can write data and all users can read data. Such as commodity information.
  • Only the management terminal can read and write data: Only the management terminal can read and write data. Such as unexposed data used in the background.

For the vast majority of cases, simply using these four permissions is not enough for us, so we need to make some judgments at the code level to make sure it behaves as we expect.

, for example, when doing a bookcase project, hope the books in the bookcase can set the view and not by a third party can be a third party check, at this moment you can only set a collection of data to “only the creator can write, everyone can read”, and through the code to control whether specific information display, such as joining a is_private fields to control.

Question and answer

How do I deploy applets?

reading

Teach you to build your own “micro vision” in 1 day

Teach you from 0 to 1 build small program audio and video

Teach you how to quickly build a press conference live program

Deep Learning technology (NLP) with a PhD from Nanyang Technological University in Singapore

This article has been authorized by the author to Tencent Cloud + community, more original text pleaseClick on the

Search concern public number “cloud plus community”, the first time to obtain technical dry goods, after concern reply 1024 send you a technical course gift package!

Massive technical practice experience, all in the cloud plus community!