Node unit CI Node lint CI Node conformace CI Security Scorecard
An open source FaaS (Function as a Service) framework based on Express for writing portable Node.js functions.
The Functions Framework lets you write lightweight functions that run in many different environments, including:
- Google Cloud Run functions
- Your local development machine
- Cloud Run and Cloud Run for Anthos
- Knative-based environments
The framework allows you to go from:
/** * Send "Hello, World!" * @param req https://expressjs.com/en/api.html#req * @param res https://expressjs.com/en/api.html#res */ exports.helloWorld = (req, res) => { res.send('Hello, World!'); };
To:
curl http://my-url
# Output: Hello, World!All without needing to worry about writing an HTTP server or complicated request handling logic.
Watch this video to learn more about the Node Functions Framework.
- Spin up a local development server for quick testing
- Invoke a function in response to a request
- Automatically unmarshal events conforming to the CloudEvents spec
- Portable between serverless platforms
Add the Functions Framework to your package.json file using npm.
npm install @google-cloud/functions-framework
-
Create an
index.jsfile with the following contents:exports.helloWorld = (req, res) => { res.send('Hello, World'); };
-
Run the following command:
Open http://localhost:8080/ in your browser and see Hello, World.-
Create a
package.jsonfile usingnpm init:npm init
-
Create an
index.jsfile with the following contents:const functions = require('@google-cloud/functions-framework'); functions.http('helloWorld', (req, res) => { res.send('Hello, World'); });
-
Now install the Functions Framework:
npm install @google-cloud/functions-framework
-
Add a
startscript topackage.json, with configuration passed via command-line arguments:"scripts": { "start": "functions-framework --target=helloWorld" }
-
Use
npm startto start the built-in local development server:npm start ... Serving function... Function: helloWorld URL: http://localhost:8080/
-
Send requests to this function using
curlfrom another terminal window:curl localhost:8080 # Output: Hello, World
-
Build a container from your function using the Functions buildpacks:
pack build \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \ --env GOOGLE_FUNCTION_TARGET=helloWorld \ my-first-function
-
Start the built container:
docker run --rm -p 8080:8080 my-first-function # Output: Serving function... -
Send requests to this function using
curlfrom another terminal window:curl localhost:8080 # Output: Hello, World!
The Node.JS runtime on Cloud Run functions utilizes the Node.JS Functions Framework. On Cloud Run functions, the Functions Framework is completely optional: if you don't add it to your
package.json, it will be installed automatically.After you've written your function, you can simply deploy it from your local machine using the
gcloudcommand-line tool. Check out the Cloud Functions quickstart.Cloud Run and Cloud Run for Anthos both implement the Knative Serving API. The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment.
You can configure the Functions Framework using command-line flags or environment variables. If you specify both, the environment variable will be ignored.
Command-line flag Environment variable Description --portPORTThe port on which the Functions Framework listens for requests. Default: 8080--targetFUNCTION_TARGETThe name of the exported function to be invoked in response to requests. Default: function--signature-typeFUNCTION_SIGNATURE_TYPEThe signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: http; accepted values:httporeventorcloudevent--sourceFUNCTION_SOURCEThe path to the directory of your function. Default: cwd(the current working directory)--log-execution-idLOG_EXECUTION_IDEnables execution IDs in logs, either trueorfalse. When not specified, default to disable. Requires Node.js 13.0.0 or later.--ignored-routesIGNORED_ROUTESA route expression for requests that should not be routed the function. An empty 404 response will be returned. This is set to `/favicon.ico You can set command-line flags in your
package.jsonvia thestartscript. For example:"scripts": { "start": "functions-framework --target=helloWorld" }
The Functions Framework can unmarshall incoming Google Cloud Functions event payloads to
dataandcontextobjects. These will be passed as arguments to your function when it receives a request. Note that your function must use theevent-style function signature:exports.helloEvents = (data, context) => { console.log(data); console.log(context); };
To enable automatic unmarshalling, set the function signature type to
eventusing a command-line flag or an environment variable. By default, the HTTP signature will be used and automatic event unmarshalling will be disabled.For more details on this signature type, check out the Google Cloud Functions documentation on background functions.
The Functions Framework can unmarshall incoming CloudEvents payloads to a
cloudeventobject. It will be passed as an argument to your function when it receives a request. Note that your function must use thecloudevent-style function signature:const functions = require('@google-cloud/functions-framework'); functions.cloudEvent('helloCloudEvents', (cloudevent) => { console.log(cloudevent.specversion); console.log(cloudevent.type); console.log(cloudevent.source); console.log(cloudevent.subject); console.log(cloudevent.id); console.log(cloudevent.time); console.log(cloudevent.datacontenttype); });
More advanced guides and docs can be found in the
docs/folder.Contributions to this library are welcome and encouraged. See CONTRIBUTING for more information on how to get started.
-