Setting up the Node.js project for HTTP functions¶
The following examples assumes that you have Node.js 20.15.1 installed you are using npm as the package manager and linux.
Project structure¶
A minimal Node.js FunctionGraph project is typically structured as follows:
/project-root
├─ src
| └─ index.js
└─ package.json
Step 1: Initialize the project¶
This project will use Koa, a popular Node.js web framework, to implement the HTTP function.
Run the following command to create a project folder.
mkdir -p scratch-http-simple/src cd scratch-http-simple
Run the following commands to initialize the Node.js project and download the koa framework.
npm init -y npm i koa
This will create following package.json file in the project root directory.
package.json¶{ "name": "scratch-http-simple", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "type": "commonjs", "dependencies": { "koa": "^3.1.2" } }
Step 2: Sample code¶
in the src folder, create a file named index.js and add the following code. For details about how to use this framework, see Koa’s guide .
Sample code:
"use strict";
const Koa = require("koa");
const app = new Koa();
app.use(async (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.listen(8000);
console.log("Node.js web server at port 8000 is running.");
Step 2a: Test the function locally
To test the function locally, run the following command in the project root directory:
node src/index.js
Then send a HTTP request to the function with the following command:
curl -X POST localhost:8000
# Result: Hello World!
curl -X POST localhost:8000/koa
# Result: Hello World, user!
Step 3: Create the bootstrap file¶
Create a file named bootstrap in the project root directory and add the following code.
/opt/function/runtime/nodejs20.15/rtsp/nodejs/bin/node $RUNTIME_CODE_ROOT/src/index.js
Step 4: Enhance package.json and build the zip package¶
To deploy the function to FunctionGraph, you need to create a zip package of the project. The Zip package should have following structure:
/filename.zip
├─ node_modules # NPM third-party dependencies (optional)
| └─ ...
├─ src
| └─ index.js # main function code file (mandatory)
├─ bootstrap # bootstrap file to start the function runtime (mandatory)
└─ package.json # NPM project management file (optional but recommended)
To create the zip package, you can enhance the package.json file as follows to include the necessary fields for FunctionGraph deployment.
{
"name": "scratch-http-simple",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "NODE_DEBUG=koa* node -harmony src/index.js",
"test": "echo $(curl -s -X POST localhost:8000)",
"postpack": "rm -f ${npm_package_name}.zip && tarball=$(npm list --depth 0 | sed 's/@/-/g; s/ .*/.tgz/g; 1q;'); tar -tf $tarball | sed 's/^package\\///' | zip -@r ${npm_package_name}.zip; rm $tarball"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"koa": "^3.1.2"
},
"bundleDependencies": [
"koa"
],
"files": [
"src/**/*",
"bootstrap"
]
}
To create the zip package, run the following command in the project root directory:
npm pack
npm pack will create a tarball package of the project with the name in the format of [package-name]-[version].tgz (e.g., scratch-http-simple-1.0.0.tgz) and includes files specified in the files field of package.json and the dependencies specified in the bundleDependencies field of package.json.
FunctionGraph requires the package to be in zip format, so you need to convert the tarball package to zip format. The package.json includes a postpack command to handle this conversion.
Note
See also Packaging Node.js Functions
Step 5: Deploy to FunctionGraph¶
In FunctionGraph console, create a function with the following parameters:
Create with: Create from scratch
Function Type: HTTP Function
Region: select the region where you want to create the function
Function Name: scratch-http-simple
leave the other parameters with default values, and click Create Function to create the function.
In the Code tab of the created function, click Upload -> local Zip to upload the generated zip package (e.g., scratch-http-simple.zip) to FunctionGraph.
Step 6: Test the Function¶
Create a test event with the following parameters:
Event Name: test-event
Event Template: API Gateway (Dedicated)
{
"body": "",
"requestContext": {
"apiId": "bc1dcffd-aa35-474d-897c-d53425a4c08e",
"requestId": "11cdcdcf33949dc6d722640a13091c77",
"stage": "RELEASE"
},
"queryStringParameters": {
"responseType": "html"
},
"httpMethod": "GET",
"pathParameters": {},
"headers": {
"accept-language": "q=0.5,en-US;q=0.3,en;q=0.2",
"accept-encoding": "gzip, deflate, br",
"x-forwarded-port": "443",
"x-forwarded-for": "103.218.216.98",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"upgrade-insecure-requests": "1",
"host": "host",
"x-forwarded-proto": "https",
"pragma": "no-cache",
"cache-control": "no-cache",
"x-real-ip": "103.218.216.98",
"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
},
"path": "/apig-event-template",
"isBase64Encoded": true
}
Click Test to execute the function and you should see the following output in the Execution Result section:
{
"body": "SGVsbG8gV29ybGQh",
"headers": {
"Content-Length": [
"12"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Thu, 19 Mar 2026 11:03:46 GMT"
]
},
"statusCode": 200,
"isBase64Encoded": true
}
The response body is Base64-encoded. After decoding, you will get the string “Hello World!”.