Quick Start
This article will introduce how to quickly create a functional cloud hosting and initiate calls from the mini-program client.
Creating Function Cloud Hosting via Template
On the cloud function interface, select "Create Cloud Function", then choose "New Function Cloud Hosting" and select the "Function Cloud Hosting Blank Template".
In the basic information on the right side of the template, enter "testfunc2" for the function name.
Click "Start Creation" to complete the function creation process and wait for the version deployment to finish.
During creation, you can refer to the template details to learn how to invoke this function later.
The current template has built-in callable functions. You can refer to the Template Project.
Invoking from the Mini Program Client
You can invoke the function in Mini Programs in the following way:
// Environment id is required for invocation and cannot be empty
const c1 = new wx.cloud.Cloud({
resourceEnv: 'environment id' // Fill in your CloudBase environment id
})
await c1.init()
const r1 = await c1.callContainer({
path: '/', // request path for the default function
header: {
'X-WX-SERVICE': 'testfunc2', // Enter the function name used during creation, testfunc2
},
// Other parameters are the same as wx.request
method: 'GET',
})
console.log(r1) // Prints Hello world!
const r2 = await c1.callContainer({
path: '/echo', // request path for the echo function
header: {
'X-WX-SERVICE': 'testfunc2', // Enter the function name used during creation, testfunc2
},
// Other parameters are the same as wx.request
method: 'POST',
data: {
a: 1,
b: 2
}
})
console.log(r2) // Prints the specific request and time
In the debugger, you can view the request results similar to the following: