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:
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:
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.
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.
context parameter
The context parameter is used to pass runtime information to your handler function. The context parameter contains the following content.
Parameter description
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.
Parameter description
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.
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: