All Projects → GoogleCloudPlatform → Functions Framework Nodejs

GoogleCloudPlatform / Functions Framework Nodejs

Licence: apache-2.0
FaaS (Function as a service) framework for writing portable Node.js functions

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Functions Framework Nodejs

Functions Framework Go
FaaS (Function as a service) framework for writing portable Go functions
Stars: ✭ 169 (-78.61%)
Mutual labels:  google-cloud, functions-as-a-service
functions-framework-java
FaaS (Function as a service) framework for writing portable Java functions
Stars: ✭ 101 (-87.22%)
Mutual labels:  google-cloud, functions-as-a-service
functions-framework-php
FaaS (Function as a service) framework for writing portable PHP functions
Stars: ✭ 186 (-76.46%)
Mutual labels:  google-cloud, functions-as-a-service
Spotty
Training deep learning models on AWS and GCP instances
Stars: ✭ 310 (-60.76%)
Mutual labels:  google-cloud
Cs329s Ml Deployment Tutorial
Code and files to go along with CS329s machine learning model deployment tutorial.
Stars: ✭ 314 (-60.25%)
Mutual labels:  google-cloud
Cloud Functions Go
Unofficial Native Go Runtime for Google Cloud Functions
Stars: ✭ 427 (-45.95%)
Mutual labels:  google-cloud
Fission
Fast and Simple Serverless Functions for Kubernetes
Stars: ✭ 6,646 (+741.27%)
Mutual labels:  functions-as-a-service
Dab
Data Augmentation by Backtranslation (DAB) ヽ( •_-)ᕗ
Stars: ✭ 294 (-62.78%)
Mutual labels:  google-cloud
Openwhisk
Apache OpenWhisk is an open source serverless cloud platform
Stars: ✭ 5,499 (+596.08%)
Mutual labels:  functions-as-a-service
Faas
OpenFaaS - Serverless Functions Made Simple
Stars: ✭ 20,820 (+2535.44%)
Mutual labels:  functions-as-a-service
Magic Modules
Automatically generate Google Cloud Platform support for OSS IaaC Projects
Stars: ✭ 358 (-54.68%)
Mutual labels:  google-cloud
Caliban
Research workflows made easy, locally and in the Cloud.
Stars: ✭ 333 (-57.85%)
Mutual labels:  google-cloud
Awesome Firebase
🔥 List of Firebase talks, tools, examples & articles! Translations in 🇬🇧 🇷🇺 Contributions welcome!
Stars: ✭ 448 (-43.29%)
Mutual labels:  google-cloud
All About Programming
Everything about programming!!
Stars: ✭ 314 (-60.25%)
Mutual labels:  google-cloud
Google Cloud Dotnet
Google Cloud Client Libraries for .NET
Stars: ✭ 651 (-17.59%)
Mutual labels:  google-cloud
Functions Framework Dart
FaaS (Function as a service) framework for writing portable Dart functions
Stars: ✭ 307 (-61.14%)
Mutual labels:  functions-as-a-service
Awesome Google Cloud
A curated list of awesome stuff for Google Cloud Platform.
Stars: ✭ 471 (-40.38%)
Mutual labels:  google-cloud
Zenko
Zenko is the open source multi-cloud data controller: own and keep control of your data on any cloud.
Stars: ✭ 353 (-55.32%)
Mutual labels:  google-cloud
Knative Tutorial
A collection of samples for Knative Serving, Knative Eventing and Knative-GCP projects.
Stars: ✭ 353 (-55.32%)
Mutual labels:  google-cloud
Laravel Google Cloud Storage
A Google Cloud Storage filesystem for Laravel
Stars: ✭ 415 (-47.47%)
Mutual labels:  google-cloud

Functions Framework for Node.js Build Status npm version npm downloads

An open source FaaS (Function as a Service) framework based on Express 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:

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.

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 spec
  • Portable between serverless platforms

Installation

Add the Functions Framework to your package.json file using npm.

npm install @google-cloud/functions-framework

Quickstarts

Quickstart: Hello, World on your local machine

  1. Create an index.js file with the following contents:

    exports.helloWorld = (req, res) => {
      res.send('Hello, World');
    };
    
  2. Run the following command:

    npx @google-cloud/functions-framework --target=helloWorld
    
  3. 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:

    npm init
    
  2. Create an index.js file with the following contents:

    exports.helloWorld = (req, res) => {
      res.send('Hello, World');
    };
    
  3. Now install the Functions Framework:

    npm install @google-cloud/functions-framework
    
  4. Add a start script to package.json, with configuration passed via command-line arguments:

      "scripts": {
        "start": "functions-framework --target=helloWorld"
      }
    
  5. Use npm start to start the built-in local development server:

    npm start
    ...
    Serving function...
    Function: helloWorld
    URL: http://localhost:8080/
    
  6. Send requests to this function using curl from another terminal window:

    curl localhost:8080
    # Output: Hello, World
    

Quickstart: Build a Deployable Container

  1. Install Docker and the pack tool.

  2. 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
    
  3. Start the built container:

    docker run --rm -p 8080:8080 my-first-function
    # Output: Serving function...
    
  4. Send requests to this function using curl from another terminal window:

    curl localhost:8080
    # Output: Hello, World!
    

Run your function on serverless platforms

Google Cloud Functions

The Node.js 10 runtime on Google Cloud Functions 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.

Cloud Run/Cloud Run on GKE

Once you've written your function, added the Functions Framework and updated your start script in package.json, all that's left is to create a container image. Check out the Cloud Run quickstart for Node.js to create a container image and deploy it to Cloud Run. You'll write a Dockerfile when you build your container. This Dockerfile allows you to specify exactly what goes into your container (including custom binaries, a specific operating system, and more).

If you want even more control over the environment, you can deploy your container image to Cloud Run on GKE. With Cloud Run on GKE, 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 on GKE 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.

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)

You can set command-line flags in your package.json via the start script. For example:

  "scripts": {
    "start": "functions-framework --target=helloWorld"
  }

Enable Google Cloud Functions Events

The Functions Framework can unmarshall incoming Google Cloud Functions event 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:

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.

Enable CloudEvents

The Functions Framework can unmarshall incoming CloudEvents 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:

exports.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);
};

To enable CloudEvents, set the signature type to cloudevent. By default, the HTTP signature will be used and automatic event unmarshalling will be disabled.

Learn how to use CloudEvents in this guide.

Advanced Docs

More advanced guides and docs can be found in the docs/ folder.

Contributing

Contributions to this library are welcome and encouraged. See CONTRIBUTING for more information on how to get started.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].