The Node.js template for OpenFaaS uses Express.js under the hood and the LTS version of Node, but provides an abstraction where you just work with an event and context object.
Do you need to customise this template?
You can customise the official templates, or provide your own. The code for this templates is available on GitHub: openfaas/templates.
The event is used to obtain the original HTTP request, and the context is used to set the HTTP response. The underlying Express.js object is an implementation detail, and so is not available to the function author.
Async/await is supported by the handler by default.
The most thorough and complete examples for JavaScript/Node for OpenFaaS are in Alex Ellis' eBook: Serverless for Everyone Else
This is an official template maintained by OpenFaaS Ltd.
As you can see from the example, when a valid JSON input is used in the request, along with an appropriate "Content-type" header, the event.body will transform into an object, which can be indexed as a dictionary.
Unit tests provide a quick and efficient way to exercise your code on your local computer, without needing to run faas-cli build or to deploy the function to a remote cluster.
With the node22 template, any unit tests that you provide will be run automatically upon each invocation of faas-cli build.
By default, an empty test step is written to package.json inside your function's handler folder, you can override this with your own command or test runner.
Set the environment variable RAW_BODY to true to set the context.body to the original request body rather than the default behavior of parsing it as JSON.
This is useful where the original body needs to be passed to the function code without any parsing or processing. For instance, when working with binary data, or verifying the signature of a webhook.
environment:RAW_BODY:true
The raw body has a default maximum size of 100KB to prevent abuse from users. This can be configured manually to deal with larger payloads:
Change the maximum size of a JSON request body by setting the environment variable MAX_JSON_SIZE. The default value is '100kb'
Note: the value must be enclosed in quotes ''
This is useful when the function is expected to receive large amounts of JSON data in a single request. For instance, when working with large data sets and complex object types.
A common use-case for static files is when you want to serve HTML, lookup information from a JSON manifest or render some kind of templates.
With the Node templates, static files and folders can just be added to the handler directory and will be copied into the function image.
To read a file e.g data.json back at runtime you can do the following:
constfs=require("fs").promises;constpath=require("path");module.exports=async(event,context)=>{if(event.path==="/static"){// Get the path to data.json in the same directory as this handlerconstdataFilePath=path.join(__dirname,"data.json");// Read the data.json fileconstfileContent=awaitfs.readFile(dataFilePath,"utf8");returncontext.status(200).succeed(fileContent);}else{returncontext.status(200).succeed("Hello from OpenFaaS!");}};
Fork the template repository and modify the template. Recommended method that allows for distribution and reuse of the template.
Pull the template and apply patches directly in the ./template/<language_name> directory. Good for quick iteration and experimentation with template modifications. The modified template can not be shared and reused. Changes may get overwritten when pulling templates again.
Add the required packages for zero code instrumentation to the template package.json:
OTEL_SERVICE_NAME sets the name of the service associated with the telemetry and is used to identify telemetry for a specific function. It can be set to any value you want be we recommend using the clear function identifier <fn-name>.<fn-namespace>.
OTEL_TRACES_EXPORTER specifies which tracer exporter to use. In this example traces are exported to console (stdout) and with otlp. The otlp option tells the instrumentation module to send the traces to an endpoint that accepts OTLP via gRPC.
setting OTEL_METRICS_EXPORTER and OTEL_LOGS_EXPORTER to none we disable the metrics and logs exporters. You can enable them if desired.
OTEL_EXPORTER_OTLP_ENDPOINT sets the endpoint where telemetry is exported to.
The NODE_OPTIONS environment variable needs to have the value --require @opentelemetry/auto-instrumentations-node/register to register and initialize the auto instrumentation module.