Help Center/ FunctionGraph/ Developer Guide/ Node.js/ Developing a Node.js Event Function
Updated on 2025-12-10 GMT+08:00

Developing a Node.js Event Function

You can develop a Node.js event function locally and upload the code file, or create a function on the FunctionGraph console and edit code online.

For details about the syntax and SDK APIs of Node.js functions, see Function Development Overview.

Constraints

  • If the first parameter returned by callback is not null, the function execution fails and the HTTP error message defined in the second parameter is returned.
  • When an APIG trigger is used, the response must be in the output format used in this example. The body only supports the following values:
    • null: The HTTP response body is empty.
    • []byte: The content in this byte array is the body of an HTTP response.
    • string: The content in this string is the body of an HTTP response.
  • When calling a function using APIG, isBase64Encoded is valued true by default, indicating that the request body transferred to FunctionGraph is encoded using Base64 and must be decoded for processing.
    The function must return characters strings by using the following structure.
    {
        "isBase64Encoded": true|false,
        "statusCode": httpStatusCode,
        "headers": {"headerName":"headerValue",...},
        "body": "..."
    }

Step 1: Creating a Node.js Function Project

  1. Open the local text editor, write the function code, and name the file index.js.
    • The following is the code for the sync handler:
      exports.handler = function (event, context, callback) {
          const error = null;
          const output = {
              'statusCode': 200,
              'headers':
              {
                  'Content-Type': 'application/json'
              },
              'isBase64Encoded': false,
              'body': JSON.stringify(event),
          }
          callback(error, output);
      }
    • The following is the code for async handler (with runtime 8.10 or later):
      exports.handler = async (event, context) => {
          const output =
          {
              'statusCode': 200,
              'headers':
              {
                  'Content-Type': 'application/json'
              },
              'isBase64Encoded': false,
              'body': JSON.stringify(event),
          }
          return output;
      }

      If your Node.js function contains an asynchronous task, use Promise to execute the task in the current invocation. You can directly return the declared Promise or await to execute it.

      The asynchronous task can be executed only before the function responds to requests.

      exports.handler =  async(event, context ) => {
          const output =
          {
              'statusCode': 200,
              'headers':
              {
                  'Content-Type': 'application/json'
              },
              'isBase64Encoded': false,
              'body': JSON.stringify(event),
          }
       
          const promise = new Promise((resolve, reject) => {
              setTimeout(() => {
                  resolve(output)
              }, 2000)
          })
          return promise;
          // another way 
          // res = await promise;
          // return res;
      }

      Asynchronous function execution:

      To execute the function asynchronously (respond immediately upon invocation while continuing task execution), you can call the API for Executing a Function Asynchronously through SDKs or APIs.

      For an APIG trigger, click its name to go to the APIG console, and select Asynchronous for the Invocation Mode. For details, see Asynchronous Invocation.

  2. Package the function project. The following uses an async handler as an example.

    After creating the function project, you get the following directory. Select all files under the directory and package them into the fss_examples_nodejs.zip file. Ensure that the function handler is under the root directory after the ZIP file is decompressed.

    You can also download the Node.js function sample project package and use it directly.

    Figure 1 Packaging the project files

Step 2: Create a Function

  1. Log in to the FunctionGraph console and click Create Function in the upper right corner.
  2. Create a Node.js event function from scratch and click Create Now as shown inFigure 2. The function details page is displayed.
    Figure 2 Creating a Node.js function

  3. Upload the fss_examples_nodejs.zip file packed in Step 1: Creating a Node.js Function Project by clicking Upload > Local ZIP, as shown in Figure 3.
    The uploaded code will be automatically deployed on the FunctionGraph console. If you have modified the code, click Deploy again.
    Figure 3 Uploading the code package

Modifying the function handler:

In the navigation pane on the left of the FunctionGraph console, choose Functions > Function List. Click the name of the function to be set. On the function details page that is displayed, choose Configuration > Basic Settings and set the Handler parameter, as shown in Figure 4.
Figure 4 Handler parameter

Step 3: Testing the Function

  1. On the Code tab, click Test. In the Configure Test Event dialog box, set test, as shown in Figure 5, and click Create.
    Figure 5 Configuring a test event
  2. Select the configured test event test and click Test.
  3. As shown in Figure 6, the Execution Result window is displayed on the right. You can check whether the function is executed successfully.
    Figure 6 Test result

Function Execution Result Description

The execution result consists of the function output, summary, and log output.

Table 1 Description of the execution result

Parameter

Successful Execution

Failed Execution

Function Output

The defined function output information is returned.

A JSON file that contains errorMessage and errorType is returned. The format is as follows:

{
    "errorMessage": "", 
    "errorType":"", 
}

errorMessage: Error message returned by the runtime.

errorType: Error type.

Summary

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Request ID, Memory Configured, Execution Duration, Memory Used, and Billed Duration are displayed.

Log Output

Function logs are printed. A maximum of 4 KB logs can be displayed.

Error information is printed. A maximum of 4 KB logs can be displayed.

Helpful Links