Function Development Overview
FunctionGraph supports the following Node.js runtimes:
- Node.js 6.10
- Node.js 8.10
- Nodejs 10.16
- Nodejs 12.13
- Node.js 16.17
- Node.js 18.15
- Node.js 20.15
Function Syntax
Node.js 6.10
export.handler = function(event, context, callback)
- handler: name of the function that FunctionGraph invokes to execute your code. The name must be consistent with that you define when creating a function.
- event: event parameter defined for the function. The parameter is in JSON format.
- context: runtime information provided for executing the function. For details, see SDK APIs.
- callback: used to return the defined err and message information to the frontend. The general syntax is callback(err, message). You can define the error or message content, for example, a character string.
Node.js 8.10 and later
Node.js 8.10 and later are compatible with the APIs of Node.js 6.10, and supports an async handler. Responses are output through return.
exports.handler = async (event, context, callback [optional]) => { return data;}
The handler of a Node.js function is in the format of [file name].[function name]. You can configure the handler on the FunctionGraph console. For example, if you set the handler to index.handler in your function, FunctionGraph will load the handler function defined in the index.js file.
Node.js Initializer
For details about the initializer, see Initializer.
The initializer is in the format of [File name].[Initializer name].
For example, if the initializer is named index.initializer, FunctionGraph loads the initializer function defined in the index.js file.
To use Node.js to build initialization logic, define a Node.js function as the initializer. The following is a simple initializer:
exports.initializer = function(context, callback) {
callback(null, '');
};
- Function name
The function name exports.initializer must be the initializer function name specified for a function.
For example, if the initializer is named index.initializer, FunctionGraph loads the initializer function defined in the index.js file.
- context
The context parameter contains the runtime information about a function. For example, request ID, temporary AK, and function metadata.
- callback
The callback parameter is used to return the invocation result. The signature of this parameter is function(err, data), which is the same as that of the common callback parameter used in Node.js. If the value of err is not null, the function will return HandledInitializationError. The value of data is invalid because no value will be returned for function initialization. You can set the data parameter to null by referring to the previous example.
Third-Party Components Integrated with the Node.js Runtime
|
Name |
Usage |
Version |
|---|---|---|
|
q |
Asynchronous method encapsulation |
1.5.1 |
|
co |
Asynchronous process control |
4.6.0 |
|
lodash |
Common tool and method library |
4.17.10 |
|
esdk-obs-nodejs |
OBS SDKs |
2.1.5 |
|
express |
Simplified web-based application development framework |
4.16.4 |
|
fgs-express |
Uses the Node.js application framework to run serverless applications and REST APIs in FunctionGraph and APIG. This component provides an example of using the Express framework to build serverless web applications or services and RESTful APIs. |
1.0.1 |
|
request |
Simplifies HTTP invocation and supports HTTPS and redirection. |
2.88.0 |
SDK APIs
Table 2 describes the context methods provided by FunctionGraph.
|
Method |
Description |
|---|---|
|
getRequestID() |
Obtains a request ID. |
|
getRemainingTimeInMilliSeconds () |
Obtains the remaining running time of a function. |
|
getAccessKey() |
Obtains the AK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. FunctionGraph has stopped maintaining the getAccessKey API in the Runtime SDK. You cannot use this API to obtain a temporary AK. |
|
getSecretKey() |
Obtains the SK (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. FunctionGraph has stopped maintaining the getSecretKey API in the Runtime SDK. You cannot use this API to obtain a temporary SK. |
|
getSecurityAccessKey() |
Obtains the SecurityAccessKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function. |
|
getSecuritySecretKey() |
Obtains the SecuritySecretKey (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function. |
|
getSecurityToken() |
Obtains the SecurityToken (valid for 24 hours) with an agency. The cache duration is 10 minutes. That is, the same content is returned within 10 minutes. To use this method, you need to configure an agency for the function. |
|
getUserData(string key) |
Uses keys to obtain the values passed by environment variables. |
|
getFunctionName() |
Obtains the name of a function. |
|
getRunningTimeInSeconds () |
Obtains the timeout of a function. |
|
getVersion() |
Obtains the version of a function. |
|
getMemorySize() |
Obtains the allocated memory. |
|
getCPUNumber() |
Obtains CPU usage of a function. |
|
getPackage() |
Obtains a function group. |
|
getToken() |
Obtains the token (valid for 24 hours) with an agency. If you use this method, you need to configure an agency for the function. |
|
getLogger() |
Obtains the logger method provided by the context and returns a log output class. Logs are output in the format of Time-Request ID-Content by using the info method. For example, use the info method to output logs: logg = context.getLogger() logg.info("hello") |
|
getAlias() |
Obtains function alias. |
As shown in Figure 1, you can use the context class in the code editor on the FunctionGraph console.
Helpful Links
- For details about how to use Node.js to develop an event function, see Developing a Node.js Event Function.
- For details about how to use Node.js to develop an HTTP function, see Developing an HTTP Function Using Node.js.
- For details about how to create a dependency package for a Node.js function, see Creating a Dependency for a Node.js Function.
- For more information about function development, such as the supported runtimes, trigger events, function project packaging specifications, and DLL referencing, see Function Development Overview.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot
