Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/googlecloudplatform/functions-framework-nodejs
FaaS (Function as a service) framework for writing portable Node.js functions
https://github.com/googlecloudplatform/functions-framework-nodejs
cloud-functions functions-as-a-service google-cloud nodejs
Last synced: 15 days ago
JSON representation
FaaS (Function as a service) framework for writing portable Node.js functions
- Host: GitHub
- URL: https://github.com/googlecloudplatform/functions-framework-nodejs
- Owner: GoogleCloudPlatform
- License: apache-2.0
- Created: 2019-04-05T10:53:39.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-13T13:08:39.000Z (6 months ago)
- Last Synced: 2024-05-16T22:46:11.714Z (6 months ago)
- Topics: cloud-functions, functions-as-a-service, google-cloud, nodejs
- Language: TypeScript
- Homepage:
- Size: 2.07 MB
- Stars: 1,276
- Watchers: 51
- Forks: 158
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Functions Framework for Node.js
[![npm version](https://img.shields.io/npm/v/@google-cloud/functions-framework.svg)](https://www.npmjs.com/package/@google-cloud/functions-framework) [![npm downloads](https://img.shields.io/npm/dm/@google-cloud/functions-framework.svg)](https://npmcharts.com/compare/@google-cloud/functions-framework?minimal=true)
[![Node unit CI][ff_node_unit_img]][ff_node_unit_link] [![Node lint CI][ff_node_lint_img]][ff_node_lint_link] [![Node conformace CI][ff_node_conformance_img]][ff_node_conformance_link] ![Security Scorecard](https://api.securityscorecards.dev/projects/github.com/GoogleCloudPlatform/functions-framework-nodejs/badge)
An open source FaaS (Function as a Service) framework based on [Express](https://expressjs.com/)
for writing portable Node.js functions -- brought to you by the Google Cloud Functions team.The Functions Framework lets you write lightweight functions that run in many
different environments, including:* [Google Cloud Functions](https://cloud.google.com/functions/)
* Your local development machine
* [Cloud Run](https://cloud.google.com/run/) and [Cloud Run for Anthos](https://cloud.google.com/anthos/run)
* [Knative](https://github.com/knative/)-based environmentsThe framework allows you to go from:
```js
/**
* 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:
```sh
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](https://youtu.be/yMEcyAkTliU?t=912) to learn more about the Node Functions Framework.
## Features
- Spin up a local development server for quick testing
- Invoke a function in response to a request
- Automatically unmarshal events conforming to the
[CloudEvents](https://cloudevents.io/) spec
- Portable between serverless platforms## Installation
Add the Functions Framework to your `package.json` file using `npm`.
```sh
npm install @google-cloud/functions-framework
```## Quickstarts
### Quickstart: Hello, World on your local machine
1. Create an `index.js` file with the following contents:
```js
exports.helloWorld = (req, res) => {
res.send('Hello, World');
};
```1. Run the following command:
```sh
npx @google-cloud/functions-framework --target=helloWorld
```1. Open http://localhost:8080/ in your browser and see _Hello, World_.
### Quickstart: Set up a new project
1. Create a `package.json` file using `npm init`:
```sh
npm init
```1. Create an `index.js` file with the following contents:
```js
const functions = require('@google-cloud/functions-framework');functions.http('helloWorld', (req, res) => {
res.send('Hello, World');
});
```1. Now install the Functions Framework:
```sh
npm install @google-cloud/functions-framework
```1. Add a `start` script to `package.json`, with configuration passed via
command-line arguments:```js
"scripts": {
"start": "functions-framework --target=helloWorld"
}
```1. Use `npm start` to start the built-in local development server:
```sh
npm start
...
Serving function...
Function: helloWorld
URL: http://localhost:8080/
```1. Send requests to this function using `curl` from another terminal window:
```sh
curl localhost:8080
# Output: Hello, World
```### Quickstart: Build a Deployable Container
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).
1. Build a container from your function using the Functions [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks):
```sh
pack build \
--builder gcr.io/buildpacks/builder:v1 \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=helloWorld \
my-first-function
```1. Start the built container:
```sh
docker run --rm -p 8080:8080 my-first-function
# Output: Serving function...
```1. Send requests to this function using `curl` from another terminal window:
```sh
curl localhost:8080
# Output: Hello, World!
```## Run your function on serverless platforms
### Google Cloud Functions
The
[Node.js 10 runtime on Google Cloud Functions](https://cloud.google.com/functions/docs/concepts/nodejs-10-runtime)
is based on the Functions Framework. On Cloud 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 `gcloud` command-line tool.
[Check out the Cloud Functions quickstart](https://cloud.google.com/functions/docs/quickstart).### Cloud Run / Cloud Run for Anthos
After you've written your function, added the Functions Framework and updated your `start` script in `package.json`, deploy it to Cloud Run with `gcloud run deploy`. Check out the [Cloud Run quickstart for Node.js](https://cloud.google.com/run/docs/quickstarts/build-and-deploy/nodejs).
If you want even more control over the environment, you can [deploy to Cloud Run for Anthos](https://cloud.google.com/anthos/run/docs/quickstarts/prebuilt-deploy-gke). With Cloud Run for Anthos, you can run your function on a GKE cluster, which gives you additional control over the environment (including use of GPU-based instances, longer timeouts and more).
### Container environments based on Knative
Cloud Run and Cloud Run for Anthos both implement the [Knative Serving API](https://www.knative.dev/docs/). The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment.
## Configure the Functions Framework
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 |
| ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` |
| `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` |
| `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` |
| `--source` | `FUNCTION_SOURCE` | The path to the directory of your function. Default: `cwd` (the current working directory) |
| `--log-execution-id`| `LOG_EXECUTION_ID` | Enables execution IDs in logs, either `true` or `false`. When not specified, default to disable. Requires Node.js 13.0.0 or later. |You can set command-line flags in your `package.json` via the `start` script.
For example:```js
"scripts": {
"start": "functions-framework --target=helloWorld"
}
```## Enable Google Cloud Functions Events
The Functions Framework can unmarshall incoming
Google Cloud Functions [event](https://cloud.google.com/functions/docs/concepts/events-triggers#events) payloads to `data` and `context` objects.
These will be passed as arguments to your function when it receives a request.
Note that your function must use the `event`-style function signature:```js
exports.helloEvents = (data, context) => {
console.log(data);
console.log(context);
};
```To enable automatic unmarshalling, set the function signature type to `event`
using 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](https://cloud.google.com/functions/docs/writing/background#cloud_pubsub_example).## Enable CloudEvents
The Functions Framework can unmarshall incoming
[CloudEvents](http://cloudevents.io) payloads to a `cloudevent` object.
It will be passed as an argument to your function when it receives a request.
Note that your function must use the `cloudevent`-style function signature:```js
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);
});
```## Advanced Docs
More advanced guides and docs can be found in the [`docs/` folder](docs/).
## Contributing
Contributions to this library are welcome and encouraged. See
[CONTRIBUTING](CONTRIBUTING.md) for more information on how to get started.[ff_node_unit_img]: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/actions/workflows/unit.yml/badge.svg
[ff_node_unit_link]: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/actions/workflows/unit.yml
[ff_node_lint_img]: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/actions/workflows/lint.yml/badge.svg
[ff_node_lint_link]: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/actions/workflows/lint.yml
[ff_node_conformance_img]: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/actions/workflows/conformance.yml/badge.svg
[ff_node_conformance_link]: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/actions/workflows/conformance.yml