This feature supports phones and tablets by default. Starting from version 5.1.0(18), support for wearables is added. From version 5.1.1(19), TVs are also supported. From version 6.1.0(23), PCs/2-in-1 devices are supported as well.
Define an entry point method as follows:
module.exports.myHandler = function(event, context, callback, logger)
myHandler: entry point method name.
event: JSON event object passed by the caller. For details, please refer to event Object.
context: function context, which encapsulates the log API, callback API, environment variable env, and more.
callback: event handling result.
logger: API for recording logs.
callback(object) must be called explicitly to send the event handling result (any object that is compatible with JSON.stringify) to AppGallery Connect, which will then convert the result into a JSON string and send it back to the caller. The function finishes execution after the callback has completed.
For complete sample code of a cloud function (Node.js 20.x), please refer to Sample Function.
You can call the logger API in your code to record logs. The API has been globally defined in the backend using global.logger. Currently, the following levels are supported:
logger.debug()
logger.error()
logger.warn()
logger.info()
You can use context.env.key in the code to access environment variables. The following is an example of obtaining the environment variable env1:
let env1 = context.env.env1;
If the environment variable is not set, undefined is returned.

You can define your function code to capture an exception, encapsulate the captured exception into an error object, and send the error object to the caller. The exception captured by the system during function execution is also encapsulated into an error object and then sent back to the caller. The error object is defined as follows:
let error = {
code: xxxxxx,
message: "xxxxxxxx"
}; In the preceding information, code indicates the result code and message indicates the result code description.
The sample code is as follows:
try {
logger.info(JSON.stringify(event));
let result = { message: "success" };
callback(result);
} catch (err) {
let error = {
code: 400,
message: err.message
};
callback(error);
} The following is a sample function:
The return value type of the myHandler() entry point method in the sample code is for reference only. You can define it as needed.
let myHandler = function (event, context, callback, logger) {
let env1 = context.env.env1;
logger.info("Test info log");
logger.warn("Test warn log");
logger.debug("Test debug log");
logger.error("Test error log");
logger.info("--------Start-------");
try {
let startTime = new Date().getTime();
let endTime = startTime;
let interval = 0;
startTime = process.uptime() * 1000;
logger.info("request: " + JSON.stringify(event.request));
logger.info("env1: " + env1);
endTime = process.uptime() * 1000;
interval = endTime - startTime;
logger.info("intervalTime: " + interval);
logger.info("--------Finished-------");
let res = new context.HTTPResponse(context.env, {
"res-type": "context.env",
"faas-content-type": "json"
}, "application/json", "200");
res.body = { "intervalTime": interval };
callback(res);
} catch (error) {
logger.error("--------Error-------");
logger.error("error: " + error);
callback(error);
}
};
module.exports.myHandler = myHandler; The Node.js deployment package you upload must use the following structure. The code file (handler.js in this example) of the handler must be in the root directory of the ZIP package, and the dependencies must be placed in the node_modules directory.
my-function.zip
|---- handler.js
|---- node_modules
|----async
|----async-listener You can run commands of the npm tool to install and manage dependencies. For example, run the npm install xxx command (no restriction on the execution path) to automatically install dependency xxx to the node_modules folder in the root directory.
