Help Center > > Developer Guide> Developing Functions> Developing Functions in Node.js

Developing Functions in Node.js

Updated at: Oct 29, 2019 GMT+08:00

Function Syntax

  • Node.js 6.10

    Use the following syntax when creating a handler function in 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 the description of 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 err or message content, for example, a character string.
    • Function handler: index.handler.

      The function handler is in the format of [file name].[function name]. For example, if you set the handler to index.handler in your function, as shown in Figure 1, FunctionGraph will load the handler function defined in the index.js file.

      Figure 1 Handler of a Node.js function
  • Node.js 8.10

    Node.js 8.10 is compatible with the APIs of Node.js 6.10, and supports an async handler.

    exports.handler = async (event, context, callback[optional]) => { return data;}

    Responses are output through return.


Table 1 lists the context methods provided by FunctionGraph.

Table 1 Context method description

Method Name



Obtains a request ID.

getRemainingTimeInMilliSeconds ()

Obtains the running time left of a function.


Obtains an access key ID (AK). An agency is required when you use this method.


Obtains a secret access key (SK). An agency is required when you use this method.

getUserData(string key)

Uses keys to obtain the values passed by environment variables.


Obtains the name of a function.

getRunningTimeInSeconds ()

Obtains the timeout of a function.


Obtains the version of a function.


Obtains the allocated memory.


Obtains the allocated number of CPUs.


Obtains a project ID.


Obtains a function group.


Obtains a token. An agency is required when you use this method.


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, a logg object is obtained through logg = context.getLogger(), and logs are output by calling the info method in"hello").

Results returned by using the getToken(), getAccessKey(), and getSecretKey() methods contain sensitive information. When using these methods, exercise caution to avoid disclosing sensitive user information.

Developing a Node.js Function

Perform the following steps to develop a Node.js function:

  1. Create a function project.

    1. Write code for printing text helloworld.

      Open the text editor, develop a helloworld function, and save the code file as helloworld.js. The code is as follows:

      var return_helloworld = function() {
      module.exports = {
          print: return_helloworld
    2. Define a FunctionGraph function.

      Open the text editor, define a function, and save the function file as index.js under the same directory as the helloworld.js file. The function code is as follows:

      var h = require('./helloworld.js');
      exports.handler = function (event, context, callback) {
          callback(null, JSON.stringify(event));
    1. 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.
    2. FunctionGraph can return only the following types of 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.

      Other: FunctionGraph returns a value as an object for JSON encoding, and uses the encoded object as the body of an HTTP response. The Content-Type header of the HTTP response is set to application/json.

  2. Package the project files.

    After creating the function project, you will have the following directory. Select all files under the directory and package them into the file, as shown in Figure 2.

    Figure 2 Packaging the project files

    In this example, the function project files are saved under the ~/Code/ directory. Select and package all files under the directory to ensure that the index.js file, the handler of your FunctionGraph function, is under the root directory when the ZIP file is decompressed.

  3. Create a FunctionGraph function and upload the code package.

    Log in to the FunctionGraph console, create a Node.js function, and upload the file, as shown in Figure 3.
    Figure 3 Uploading the code package
    1. The index of the handler must be consistent with the file name of your function, because the name will help to locate the function file.
    2. The handler is a function name, which must be consistent with that defined in the index.js file.
    3. After you upload the file to OBS, when the function is triggered, FunctionGraph decompresses the file to locate the function file through index and locate the function in the index.js file through handler, and then executes the function.

  4. Test the function.

    1. Create a test event.

      On the function details page that is displayed, choose Select test event > Configure test event. Configure the test event information, as shown in Figure 4, and then click Save.

      Figure 4 Configuring a test event
    2. On the function details page, select the configured test event, and click Test.

  5. View the function test result.

    The function test result consists of three parts: function output (returned by callback), summary, and logs (output by using the console.log or getLogger() method), as shown in Figure 5.

    Figure 5 Test result

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?

Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel