Node Functions
Overview
Node Functions simplify your backend development process, providing a Node.js runtime that seamlessly integrates with front-end projects. You can build and deploy dynamic APIs and complex business logic in a unified project. The platform will automatically handle version control, build and deployment, and intelligently scale based on business workload, helping you quickly launch and achieve continuous delivery.
Strengths
Complete Node.js ecosystem: You can directly use the massive modules in the npm ecosystem, easily integrate various third-party libraries and tools, and meet complex business needs.
Full stack development experience: No need to separate frontend and backend projects. You can complete development and deployment in one project, drastically enhancing collaboration efficiency.
Routing as a service: Define API routes through the file system, implement backend logic for rapid development and deployment, and manage backend services as conveniently as frontend pages.
Quick Start
Create hello.ts in the ./node-functions directory and use the following example code to create your first Node Functions:
export default function onRequest(context) {return new Response('Hello from Node Functions!');}
Function Debugging
1. Install EdgeOne CLI:
npm install -g edgeone
2. Local development: Execute
edgeone pages dev
in the Pages code project to start the local service and perform function debugging.3. Function release: Push code to the remote repository for CI automated build and function release.
Routing
Node Functions generates access routes based on the
/node-functions
directory structure. You can create subdirectories at any level under the /node-functions directory in the project repository. See the example below....node-functions├── index.js├── hello-pages.js├── helloworld.js├── api├── users├── list.js├── geo.js├── [id].js├── visit├── index.js├── [[default]].js...
The above directory file structure will generate the following routes after EdgeOne Pages platform construction. These routes map Pages URLs to
/node-functions
files. When a client accesses the URL, it will trigger the corresponding file code to run:File path | Routing |
/node-functions/index.js | example.com/ |
/node-functions/hello-pages.js | example.com/hello-pages |
/node-functions/helloworld.js | example.com/helloworld |
/node-functions/api/users/list.js | example.com/api/users/list |
/node-functions/api/users/geo.js | example.com/api/users/geo |
/node-functions/api/users/[id].js | example.com/api/users/1024 |
/node-functions/api/visit/index.js | example.com/api/visit |
/node-functions/api/[[default]].js | example.com/api/books/list example.com/api/books/1024 example.com/api/... |
Note:
The trailing slash / in the route is optional.
/hello-pages
and /hello-pages/
will be routed to /node-functions/hello-pages.js.If no Edge Functions route is matched, client requests will be routed to the static resource of Pages.
- Routes are case-sensitive. /helloworld will be routed to /node-functions/helloworld.js and cannot be routed to /node-functions/HelloWorld.js.
Dynamic routing
Node Functions support dynamic routing. In the above example, the first-level dynamic path is /node-functions/api/users/[id].js, and the multi-level dynamic path is /node-functions/api/[[default]].js. See the usage below:
File path | Routing | Match |
/node-functions/api/users/[id].js | example.com/api/users/1024 | Yes |
| example.com/api/users/vip/1024 | No |
| example.com/api/vip/1024 | No |
/node-functions/api/[[default]].js | example.com/api/books/list | Yes |
| example.com/api/1024 | Yes |
| example.com/v2/vip/1024 | No |
Built-in route framework
Built-in routing of the Express/Koa framework needs to be mounted to the configured routing of node-functions.
Note:
For the Express/Koa framework, the entry function file name must be in the format [[]], such as [[default]].js.
For example, to create an express application in the directory node-functions/api/*, use [[default]].js as the entry, and place all express-related routing inside [[default]].js:
node-functions└── api├── [[default]].js # express.js/koa.js entry file, containing built-in route definitions of the framework└── users.js # ordinary node functions route, access path is /api/users
Function Handlers
Functions Handlers can be used to create custom request handlers for Pages and define RESTful APIs to implement full-stack applications. The following Handler methods are supported:
Handlers method | Description |
onRequest (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( GET , POST , PATCH , PUT , DELETE , HEAD , OPTIONS ) |
onRequestGet (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( GET ) |
onRequestPost (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( POST ) |
onRequestPatch (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( PATCH ) |
onRequestPut (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( PUT ) |
onRequestDelete (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( DELETE ) |
onRequestHead (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( HEAD ) |
onRequestOptions (context: EventContext): Response | Promise<Response> | Match HTTP Methods ( OPTIONS ) |
EventContext object description
The context object passed to Function Handlers methods contains these properties:
uuid: EO-LOG-UUID represents the unique identifier of an EO request
params: dynamic routing
/node-functions/api/users/[id].js
parameter valueexport function onRequestGet(context) {return new Response(`User id is ${context.params.id}`);}
env: Pages environment variables
clientIp: client IP address
server:
region: region code of the deployment location
requestId: request ID for log tracking
geo: client geographic location
Use Limits
Content | Limit | Description |
Code package size | 128 MB | Single function code package size supports up to 128 MB |
Request body size | 6 MB | Client request body supports up to 6 MB |
Execution Duration | 30s | Wall time |
Development Language | Node | Currently only support Node.js, default version v20.x |
Log Analysis
The Pages console provides basic log viewing. Developers can view basic log information for Node Functions calls, quickly detect and resolve anomalies or errors in API calls through logs. For detailed directions, view the document Log Analysis.
Example Template
Connect to a third-party MySQL database:
Preview address: https://mysql-template.edgeone.app
source code address: https://github.com/TencentEdgeOne/mysql-template/
Use the Express framework:
Preview address: https://express-template.edgeone.app
source code address: https://github.com/TencentEdgeOne/express-template/
Use the Koa framework:
Preview address: https://koa-template.edgeone.app
Source code address: https://github.com/TencentEdgeOne/koa-template/