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.
- Run the following command to create a project folder.
mkdir koa-example && cd koa-example
- 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.
- 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.') - 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
- 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
- Log in to the FunctionGraph console and click Create Function in the upper right corner.
- 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
- For details about how to develop functions in Node.js, see Function Development Overview.
- 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 create a Node.js function dependency, see Creating a Dependency for a Node.js Function.
- For details about HTTP functions, see Creating an HTTP 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