Currently, the following versions of Node.js programming language are supported:
The Node.js function form is generally as follows:
module.exports = (event,context,callback)=>{
console.log(event);
callback(null, {code:0});
}
Ormodule.exports = async (event,context)=>{
console.log(event);
return { code:0 };
}
exports.main_handler = (event, context, callback) => {
console.log("Hello World");
console.log(event);
console.log(context);
callback(null, event);
};
When you create an SCF function, you need to specify an execution method. If the Node.js programming language is used, the execution method is similar to index.main_handler
, where index
indicates that the executed entry file is index.js
, and main_handler
indicates that the executed entry function is main_handler
. When submitting the zip code package by uploading the zip file locally or through COS, please make sure that the root directory of the package contains the specified entry file, the file contains the entry function specified by the definition, and the names of the file and function match those entered in the trigger; otherwise, execution will fail as the entry file or entry function cannot be found.
The input parameters in the Node.js environment include event
, context
, and callback
, where callback
is optional.
return
keyword for return, while non-async entry functions use the callback
input parameter for return.Your handler can use the callback
input parameter or the return
keyword in the code to return information. The support conditions for using callback
or return
for return are as follows:
Node.js Version | Callback | Return |
---|---|---|
6.10 | Supported | Not supported |
8.9 | Supported | Supported |
10.15 | Non-async entry function | Async entry function |
12.16 |
callback
is used for return, the syntax will be as follows:callback(Error error, Object result);
null
in case of success.JSON.stringify
for serialization to JSON format.return
keyword is used for return, you can directly use return object
to return an object or value.callback
or return
is not invoked in the code, the SCF backend will make the invocation implicitly and return null
.The return value will be handled differently depending on the type of invocation when the function is invoked. The return value of a sync invocation will be serialized to JSON format and then returned to the invoker, while the return value of an async invocation will be discarded. In addition, the return value will be displayed at the ret_msg
position in the function log for both sync and async invocations.
In the runtime of Node.js 10.15 and 12.16, sync execution return and async event processing can be performed separately:
Note:
- SCF logs are collected and processed after the entire execution process ends. Therefore, before the sync execution process is completed and the result is returned, logs and operation information such as time used and memory utilization cannot be provided in the SCF return information. You can query the detailed information in logs by using
Request Id
after the actual function execution process is completed.- The function execution duration is calculated based on the async event execution duration. If the async event queue cannot get empty or its execution cannot be completed, function timeout will occur. In this case, the invoker may have received the correct response result of the function, but the execution status of the function will still be marked as failure due to timeout, and the timeout period will be calculated as the execution duration.
The sync and async execution attributes, return time, and execution duration in Node.js 10.15 and 12.16 are as shown below:
Use the following sample code to create a function, where the setTimeout
method is used to set a function that will be executed in 2 seconds:
'use strict';
exports.main_handler = (event, context, callback) => {
console.log("Hello World")
console.log(event)
setTimeout(timeoutfunc, 2000, 'data');
callback(null, event);
};
function timeoutfunc(arg) {
console.log(`arg => ${arg}`);
}
After saving the code, you can invoke this function through the testing feature in the console or the Invoke
API. You can see that the function can return the result in a response period below 1 second.
You can see the following statistics in the function execution log:
START RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffc
Event RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffc
2020-03-18T09:16:13.440Z 1d71ddf8-5022-4461-84b7-e3a152403ffc Hello World
2020-03-18T09:16:13.440Z 1d71ddf8-5022-4461-84b7-e3a152403ffc { key1: 'test value 1', key2: 'test value 2' }
2020-03-18T09:16:15.443Z 1d71ddf8-5022-4461-84b7-e3a152403ffc arg => data
END RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffc
Report RequestId: 1d71ddf8-5022-4461-84b7-e3a152403ffc Duration:2005ms Memory:128MB MemUsage:13.425781MB
A 2,005-ms execution period is logged. You can also find in the log that the arg => data
is output 2 seconds later, which shows that the relevant async operations are executed in the current invocation after the execution of the sync process is completed, while function invocation ends after execution of the async task is completed.
Both Node.js 8.9 and 6.10 support async events; however, immediate return after sync processing is completed is not supported. After the callback is invoked in the entry function, the SCF backend will wait until processing of async events is completed and the event queue gets empty before returning the result for the function invocation and ending this invocation.
Therefore, for the following code:
'use strict';
exports.callback_handler = function(event, context, callback) {
console.log("event = " + event);
console.log("before callback");
setTimeout(
function(){
console.log(new Date);
console.log("timeout before callback");
},
500
);
callback(null, "success callback");
console.log("after callback");
};
The actual log output is:
2018-06-14T08:07:16.545Z f3cb1ef4-6fa9-11e8-aa8a-525400c7c826 event = [object Object]
2018-06-14T08:07:16.546Z f3cb1ef4-6fa9-11e8-aa8a-525400c7c826 before callback
2018-06-14T08:07:16.546Z f3cb1ef4-6fa9-11e8-aa8a-525400c7c826 after callback
2018-06-14T08:07:17.047Z f3cb1ef4-6fa9-11e8-aa8a-525400c7c826 2018-06-14T08:07:17.047Z
2018-06-14T08:07:17.048Z f3cb1ef4-6fa9-11e8-aa8a-525400c7c826 timeout before callback
As you can see, the async task set by setTimeout
is executed after the callback is executed, and the function does not actually return until the async task is completed.
Some externally referenced libraries may cause the event loop to never get empty, which may lead to timeout due to failed return of the function in some cases. In order to avoid the impact of external libraries, you can control the function return timing by turning off event loop wait. You can modify the default callback behavior in the following way to avoid waiting for the event loop to get empty.
context.callbackWaitsForEmptyEventLoop
to false
.context.callbackWaitsForEmptyEventLoop = false;
before the callback
callback is executed, the SCF backend can freeze the process immediately after the callback
callback is invoked and return immediately after the sync process is completed without waiting for the event in the event loop.You can use the following statements in the program to output a log:
The output can be viewed at the log
location in the function log.
For more information, please see Dependency Installation and Online Dependency Installation.
SCF's runtime environment already contains the COS SDK for Node.js, and the specific version is cos-nodejs-sdk-v5
.
The COS SDK can be referenced and used within the code as follows:
var COS = require('cos-nodejs-sdk-v5');
For more information on how to use the COS SDK, please see COS SDK for Node.js.
The following libraries are supported in Node.js 12.16 runtime:
Library Name | Version |
---|---|
cos-nodejs-sdk-v5 | 2.5.20 |
base64-js | 1.3.1 |
buffer | 5.5.0 |
crypto-browserify | 3.12.0 |
ieee754 | 1.1.13 |
imagemagick | 0.1.3 |
isarray | 2.0.5 |
jmespath | 0.15.0 |
lodash | 4.17.15 |
microtime | 3.0.0 |
npm | 6.13.4 |
punycode | 2.1.1 |
puppeteer | 2.1.1 |
qcloudapi-sdk | 0.2.1 |
querystring | 0.2.0 |
request | 2.88.2 |
sax | 1.2.4 |
scf-nodejs-serverlessdb-sdk | 1.1.0 |
tencentcloud-sdk-nodejs | 3.0.147 |
url | 0.11.0 |
uuid | 7.0.3 |
xml2js | 0.4.23 |
xmlbuilder | 15.1.0 |
The following libraries are supported in Node.js 10.15 runtime:
Library Name | Version |
---|---|
cos-nodejs-sdk-v5 | 2.5.14 |
base64-js | 1.3.1 |
buffer | 5.4.3 |
crypto-browserify | 3.12.0 |
ieee754 | 1.1.13 |
imagemagick | 0.1.3 |
isarray | 2.0.5 |
jmespath | 0.15.0 |
lodash | 4.17.15 |
microtime | 3.0.0 |
npm | 6.4.1 |
punycode | 2.1.1 |
puppeteer | 2.0.0 |
qcloudapi-sdk | 0.2.1 |
querystring | 0.2.0 |
request | 2.88.0 |
sax | 1.2.4 |
scf-nodejs-serverlessdb-sdk | 1.0.1 |
tencentcloud-sdk-nodejs | 3.0.104 |
url | 0.11.0 |
uuid | 3.3.3 |
xml2js | 0.4.22 |
xmlbuilder | 13.0.2 |
The following libraries are supported in the Node.js 8.9 runtime:
Library Name | Version |
---|---|
cos-nodejs-sdk-v5 | 2.5.8 |
base64-js | 1.2.1 |
buffer | 5.0.7 |
crypto-browserify | 3.11.1 |
ieee754 | 1.1.8 |
imagemagick | 0.1.3 |
isarray | 2.0.2 |
jmespath | 0.15.0 |
lodash | 4.17.4 |
npm | 5.6.0 |
punycode | 2.1.0 |
puppeteer | 1.14.0 |
qcloudapi-sdk | 0.1.5 |
querystring | 0.2.0 |
request | 2.87.0 |
sax | 1.2.4 |
tencentcloud-sdk-nodejs | 3.0.56 |
url | 0.11.0 |
uuid | 3.1.0 |
xml2js | 0.4.17 |
xmlbuilder | 9.0.1 |
Library Name | Version |
---|---|
base64-js | 1.2.1 |
buffer | 5.0.7 |
cos-nodejs-sdk-v5 | 2.0.7 |
crypto-browserify | 3.11.1 |
ieee754 | 1.1.8 |
imagemagick | 0.1.3 |
isarray | 2.0.2 |
jmespath | 0.15.0 |
lodash | 4.17.4 |
npm | 3.10.10 |
punycode | 2.1.0 |
qcloudapi-sdk | 0.1.5 |
querystring | 0.2.0 |
request | 2.87.0 |
sax | 1.2.4 |
tencentcloud-sdk-nodejs | 3.0.10 |
url | 0.11.0 |
uuid | 3.1.0 |
xml2js | 0.4.17 |
xmlbuilder | 9.0.1 |
For more information on how to use relevant features, please see the following documents:
Was this page helpful?