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

Developing an HTTP Function Using Node.js

This section uses the Koa framework as an example to describe how to develop an HTTP function using Node.js.

Constraints

  • HTTP functions can only use APIG or APIC triggers.

    According to the forwarding protocol between FunctionGraph and APIG/APIC, a valid HTTP function response must contain body(String), statusCode(int), headers(Map), and isBase64Encoded(boolean). By default, the response is encoded using Base64. The default value of isBase64Encoded is true. The same applies to other frameworks.

  • By default, port 8000 is enabled for HTTP functions.
  • When calling a function using the APIG trigger, 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": "..."
    }

Prerequisites

You have installed the Node.js environment on the local OS. You are advised to create Node.js dependencies in the EulerOS environment.

Deploying the Koa Framework Using an HTTP Function

Koa is a Node.js-based web development framework, which is mainly used to build efficient and scalable web applications.

  1. Run the following command to create a project folder.
    mkdir koa-example && cd koa-example
  2. Run the following commands to initialize the Node.js project and download the koa framework.
    npm init -y
    npm i koa

    After the commands are executed, the node_modules folder and the package.json and package-lock.json files are added to the folder.

  3. Create the index.js file to reference the Koa framework. For details about how to use this framework, see Koa's guide.
    Sample code:
    const Koa = require("koa");
    const app = new Koa();
    const main = (ctx) = >{
        if (ctx.request.path == ("/koa")) {
            ctx.response.type = "application/json";
            ctx.response.body = "Hello World, user!";
            ctx.response.status = 200;
        } else {
            ctx.response.type = "application/json";
            ctx.response.body = 'Hello World!';
            ctx.response.status = 200;
        }
    };
    app.use(main);
    app.listen(8000, '127.0.0.1');
    console.log('Node.js web server at port 8000 is running.')
  4. You have prepared a bootstrap file as the startup file of the HTTP function. Add the following content in the file:
    /opt/function/runtime/nodejs20.15/rtsp/nodejs/bin/node $RUNTIME_CODE_ROOT/index.js
    • /opt/function/runtime/nodejs20.15/rtsp/nodejs/bin/node: path of the Node.js compilation environment.
    • $RUNTIME_CODE_ROOT: system variable that represents the /opt/function/code path for storing project code in a container.
    • index.js: project file created in 3. You can also define a custom name.

    Table 1 lists the supported Node.js versions and the corresponding paths.

    Table 1 Node.js paths

    Language

    Path

    Node.js 6

    /opt/function/runtime/nodejs6.10/rtsp/nodejs/bin/node

    Node.js 8

    /opt/function/runtime/nodejs8.10/rtsp/nodejs/bin/node

    Node.js 10

    /opt/function/runtime/nodejs10.16/rtsp/nodejs/bin/node

    Node.js 12

    /opt/function/runtime/nodejs12.13/rtsp/nodejs/bin/node

    Node.js 14

    /opt/function/runtime/nodejs14.18/rtsp/nodejs/bin/node

    Node.js 16

    /opt/function/runtime/nodejs16.17/rtsp/nodejs/bin/node

    Node.js 18

    /opt/function/runtime/nodejs18.15/rtsp/nodejs/bin/node

    Node.js 20

    /opt/function/runtime/nodejs20.15/rtsp/nodejs/bin/node

  5. Compress all project files and the bootstrap file into a ZIP package. Ensure that the project file is in the root directory after the decompression.
    Figure 1 Packaging all files
  6. Log in to the FunctionGraph console and click Create Function in the upper right corner.
  7. Create an HTTP function from scratch and upload the ZIP file to the Code tab.

    After the function is created, you can use the Koa framework to develop applications. The framework provides the infrastructure for processing requests, and your custom application code defines the specific service logic.

Helpful Links