You need to enable JavaScript to run this app.
Volcano Engine Function as a Service

Volcano Engine Function as a Service

Copy page
Download PDF
Node.js runtime
Development methods
Copy page
Download PDF
Development methods
For Node.js functions, veFaaS uses the handler entry function defined in the index.js file that you provide to start the function service process and handle request events. The sample code is as follows:​
​
// vefaas-nodejs14-default​
exports.handler = async function handler (event, context) {​
return {​
statusCode: 200,​
headers: {'Content-Type': 'application/json'},​
body: JSON.stringify({ 'vefaas-nodejs14-default': 'hello world'}),​
};​
};​
​
If your function has global initialization logic, such as establishing a connection to a database or initializing downstream clients, we recommend adding an initializer function in addition to the handler function to initialize function instances. The sample code is as follows:​
​
// vefaas-nodejs14-default​
exports.handler = async function handler (event, context) {​
return {​
statusCode: 200,​
headers: {'Content-Type': 'application/json'},​
body: JSON.stringify({ 'vefaas-nodejs14-default': 'hello world'}),​
};​
};​
​
// vefaas will try to do initializer when start up the instance, to avoid cold start from user perspective.​
// initializer will only be executed for one time when there is no exception.​
// otherwise initializer will be executed again before every invoke, till successfully inited.​
exports.initializer = async function (context) {​
console.log('initializer begin');​
// replace the following line, with the real init logic​
await new Promise(resolve => setTimeout(resolve, 3 * 1000));​
console.log('initializer end');​
};​
​
Entry function: handler​
The handler is the entry point for the function's request processing logic. The handler contains your core business logic. It is used to process event requests and ultimately returns an optional return value. The handler function signature is as follows.​
​
exports.handler = async function handler (event, context) {​
return {​
statusCode: 200,​
headers: {'Content-Type': 'application/json'},​
body: JSON.stringify({ 'vefaas-nodejs14-default': 'hello world'}),​
};​
};​
​
When veFaaS triggers your handler function, the Node.js runtime passes two parameters to your handler function: event and context.​
Event parameter​
The event parameter is used to pass trigger event data to your handler function. The type of the event parameter is Object.​
The specific content of the event parameter varies depending on the trigger event type. For details, see Trigger event message structure.​
context parameter​
The context parameter is used to pass runtime information to your handler function. The context parameter contains the following content.​
​
{​
"requestId": "888590af-cb2e-4ad4-86b3-2b03d4a7****", # Request ID​
}​
​
Parameter description​
​
Parameter​
Description​
requestId​
The system-generated request UUID. Retrieve it from the context passed into the handler function.​
​
Return value​
The response value of the handler function is of Object type. Only the three key-value pairs statusCode, headers, and body in the response value will finally be returned to the requester.​
If you do not return any value, veFaaS will return a default HTTP status code of 200.​
The sample code for the handler function's response value is as follows.​
​
{​
"statusCode": 200, // Status code, same semantics as HTTP status codes. Defaults to 200.​
"headers": { // Optional. The response headers returned by the HTTP trigger.​
"Content-Type": "application/json"​
},​
"body": JSON.stringify({​
"message": "hello world" // Optional. The response body returned by the HTTP trigger.​
})​
}​
​
Parameter description​
​
Parameter​
Required​
Description​
statusCode​
Optional​
The API response code has the same semantics as the standard HTTP status code.​
If the parameter value is not provided, the default response code 200 is returned.​
headers​
Optional​
Response header. It includes the following sub-parameter: Content-Type: the body type. We recommend using the default value application/json.​
body​
Optional​
Response body. It includes the following sub-parameter: message: the content of the returned message.​
​
Initialization function: Initializer​
The initializer is the entry point for the function's initialization logic. For instance-level global initialization within a single function, we recommend using the initializer function. The initializer function signature currently supported by veFaaS is shown below. Users can indicate initialization failure by returning an error.​
​
exports.initializer = async function (context) {​
console.log('initializer begin');​
// replace the following line, with the real init logic​
await new Promise(resolve => setTimeout(resolve, 3 * 1000));​
console.log('initializer end');​
};​
​
Using environment variables​
You can configure the environment variables required for function runtime in the console and read the corresponding environment variables in your code for function processing logic. veFaaS injects the configured environment variables into the container where the function runs, and the program can retrieve them via process.env.​
For example, if you configure an environment variable with the key-value pair HELLO: VE_FAAS, the following sample code demonstrates how to read this environment variable in the runtime:​
​
console.log(process.env.HELLO); // Output: VE_FAAS​
​
​
Last updated: 2026.08.19 14:06:05