Cloud functions support Node.js development. You can submit your code to run in the cloud and call the function from a client using the APIs provided by EMAS Serverless. You can also use APIs within your cloud function to directly call data storage and file storage service resources.
You can call other cloud functions using `ctx.mpserverless.function.invoke`. For more information, see invoke.
Step 1: Create a cloud function
Log on to the EMAS Management Console. To open the Serverless console, select Serverless and click Enter.
In the left navigation pane, click Cloud Functions.
Click New Function.
Enter a function name.
The function name must be 1 to 30 characters in length. The name can contain letters, digits, underscores (_), and hyphens (-). The name cannot start with a digit or a hyphen.
ImportantThe function name must match the name of the Node.js code package that you upload.
Select a runtime. For more information, see List of supported runtimes.
Select the function memory. The supported values are 128 MB, 256 MB, 512 MB, 1024 MB, and 2048 MB.
Enter a Description.
Click OK.
Step 2: Define the cloud function
The directory structure of the cloud function is as follows.
In this structure, index.js is the entry file for the
getImageListcloud function.└── getImageList └── index.jsNoteThe function code package is decompressed to the temporary path /tmp/function. To open a file in the code package, you must add this temporary path before the file's relative path. Otherwise, the file cannot be found and an error occurs. For example, the full path of the index.js file in the preceding example is /tmp/function/getImageList/index.js.
Write the cloud function code.
The following code sample shows how to query the
imagesdatabase for image records uploaded by a specific user.module.exports = async (ctx) => { const images = await ctx.mpserverless.db.collection('images').find({ owner: ctx.args.username }); return { images }; };NoteThe return value of a cloud function must be in JSON format.
Step 3: Deploy the cloud function
Log on to the EMAS Management Console. Select Serverless and click Enter.
In the left navigation pane, select Cloud Functions.
Click the name of the function you created.
On the Deployment Management tab, click Upload JS Package or Update JS Package, and then select the code package.
NoteThe uploaded code package must meet the following requirements:
The name of the code package must be the same as the function name that you created in the console.
The code package must be a .zip file.
The code package must contain an index.js file.
If you reference third-party packages, the code package must include the node_modules folder.
The size of a single function code package cannot exceed 500 MB after decompression.
After uploading the package, click Deploy Code.

Step 4: Execute the cloud function
Click Code Execution. The Execute page opens on the right.
In the Runtime Parameters area, enter the input parameters in JSON format, as shown in the figure below.
You can view the execution result in the Execution Result area.
Step 5: Use an SDK to call the cloud function
After the cloud function code is deployed, you can call the function from the client using the mpserverless.function.invoke method.
The following code sample shows how to retrieve the image records that are uploaded by a user and then update the page data.
// Import the MPServerless module
const MPServerless = require('@alicloud/mpserverless-sdk');
// Initialize the MPServerless object
appId: '1234456789', // Mini program application ID
const mpServerless = new MPServerless({
uploadFile: my.uploadFile,
request: my.request,
getAuthCode: my.getAuthCode,
}, {
spaceId: 'db4dd657-7041-470a-90xxxxx', // Service space ID
clientSecret: '6c3c86xxxx6', // Service space secret key
endpoint: 'https://api.next.bspapp.com', // Service space endpoint. Obtain this from the miniapp serverless console.
});
// Call the getImageList cloud function
mpServerless.function.invoke('getImageList', {
username: 'Vincent',
}).then((res) => {
if (res.success && res.result) {
this.setData({ imageList: res.result.images });
}
}).catch(console.error);
Step 6: View logs
Log on to the EMAS Management Console. Select Serverless and click Enter.
In the navigation pane on the left, select Cloud Functions.
Click the name of the function you created.
Click Logs.
You can filter the logs by running time, status, Request ID, or other information.