A brief introduction to

WeChat small program cloud development is a one-stop back-end cloud service based on Serverless, covering functions, databases, storage, CDN and other services without back-end operation and maintenance. Cloud-based development can invoke all open capabilities of WeChat without authentication.

The premise to prepare

  • WeChat Developer Tools

Create an environment

Open the small program project and click “Cloud Development” on the toolbar to open it:

Follow the dialog prompts to create the cloud environment:

Select Default Free Quota:

“Submit Order”, the creation is complete.

From there, you can open the “Cloud Development Console” :

Creating Cloud Functions

Cloud function is a piece of code running in the cloud. It does not need to manage the server, but can be written in the development tool and deployed with one click to run the back-end code.

First, configure the applet project. Edit project.config.json to add cloudFunctionRoot:

{/ /... "cloudfunctionRoot": "./cloudfunction/", "cloudfunctionTemplateRoot": "cloudfunctionTemplate" }

And in the root directory of the new directory cloudFunction, the directory icon will become “cloud directory icon”.

Then, right-click the cloud function root directory and say “New Node.js Cloud Function” :

Enter the name of the cloud function and the template will be created:

Using cloud functions

Edit index.js to sum:

// const cloud = require("wx-server-sdk"); cloud.init(); // export.main = async (event, context) => {const WxContext = cloud.getWxContext (); return { sum: event.a + event.b, }; };
  • eventIs the event that fires the cloud function. Small program side call, is its request parameters.
  • returnReturns the result of the cloud function calculation. Small program side call, is its response content.

Taro calls the cloud function

Taro is an open cross-end cross-framework solution that supports the development of WeChat/JD/Baidu/Alipay/Bytedance /QQ applet /H5 applications using frameworks such as React/Vue/Nerv.

Quickly create small program application, visible Taro3 quick start.

Taro uses cloud development, and first needs to initialize the cloud environment:

import Taro from "@tarojs/taro";

Taro.cloud.init({
  env: "gocoding-xxx",
});

Where env environment ID, visible “Settings” of “Cloud Development Console” :

Then, call the cloud function:

Taro.cloud
  .callFunction({
    name: "photo-lucky",
    data: {
      a: 1,
      b: 2,
    },
  })
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });
  • nameIs the name of the cloud function.
  • dataIs the request parameter corresponding to the cloud functionevent.

Deploy the cloud function

Right-click the cloud function directory and select “Upload and Deploy” :

Compile and run the small program, you can see the printed results:

Debugging Cloud Functions

Open Cloud Functions in the Cloud Development Console:

Open “Local Debugging” and select “Open Local Debugging” on the right:

After that, the applet calls the cloud function and goes into “local debugging.”

Cloud development services

By default, the cloud function template requires the WX-Server-SDK, which is a library provided by WeChat that helps you manipulate databases, storage, and other cloud functions in the cloud function. The use of the Wx-Server-SDK can be seen in the use of the Wx-Server-SDK in cloud functions.

other

The cloud function is a Node.js application that was intended to be developed and run directly in TypeScript. See:

  • Node.js QuickStart: https://basarat.gitbook.io/ty…

But the default main entry is index.js. I tried the configuration, but it didn’t work. The error is as follows:

So, to use TypeScript, you need TSC to compile and publish JS before debugging.

reference

  • Development of cloud
  • Taro3 starts fast

conclusion

Welcome to pay attention to GoCoding official account, share practical tips and knowledge in daily Coding!