Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FaaS (Function as a service) framework for writing portable Node.js functions

License

NotificationsYou must be signed in to change notification settings

GoogleCloudPlatform/functions-framework-nodejs

 
 

Repository files navigation

npm versionnpm downloads

Node unit CINode lint CINode conformace CISecurity Scorecard

An open source FaaS (Function as a Service) framework based onExpressfor 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 manydifferent 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 requesthandling logic.

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

Installation

Add the Functions Framework to yourpackage.json file usingnpm.

npm install @google-cloud/functions-framework

Quickstarts

Quickstart: Hello, World on your local machine

  1. Create anindex.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. Openhttp://localhost:8080/ in your browser and seeHello, World.

Quickstart: Set up a new project

  1. Create apackage.json file usingnpm init:

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

    constfunctions=require('@google-cloud/functions-framework');functions.http('helloWorld',(req,res)=>{res.send('Hello, World');});
  3. Now install the Functions Framework:

    npm install @google-cloud/functions-framework
  4. Add astart script topackage.json, with configuration passed viacommand-line arguments:

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

    npm start...Serving function...Function: helloWorldURL: http://localhost:8080/
  6. Send requests to this function usingcurl from another terminal window:

    curl localhost:8080# Output: Hello, World

Quickstart: Build a Deployable Container

  1. InstallDocker and thepack tool.

  2. Build a container from your function using the Functionsbuildpacks:

    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 usingcurl from another terminal window:

    curl localhost:8080# Output: Hello, World!

Run your function on serverless platforms

Google Cloud Functions

TheNode.js 10 runtime on Google Cloud Functionsis based on the Functions Framework. On Cloud Functions, the Functions Frameworkis completely optional: if you don't add it to yourpackage.json, it will beinstalled automatically.

After you've written your function, you can simply deploy it from your localmachine using thegcloud command-line tool.Check out the Cloud Functions quickstart.

Cloud Run / Cloud Run for Anthos

After you've written your function, added the Functions Framework and updated yourstart script inpackage.json, deploy it to Cloud Run withgcloud run deploy. Check out theCloud Run quickstart for Node.js.

If you want even more control over the environment, you candeploy to Cloud Run for Anthos. 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 theKnative 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 orenvironment variables. If you specify both, the environment variable will beignored.

Command-line flagEnvironment variableDescription
--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:http orevent orcloudevent
--sourceFUNCTION_SOURCEThe path to the directory of your function. Default:cwd (the current working directory)

You can set command-line flags in yourpackage.json via thestart script.For example:

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

Enable Google Cloud Functions Events

The Functions Framework can unmarshall incomingGoogle Cloud Functionsevent payloads todata andcontext objects.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 toeventusing a command-line flag or an environment variable. By default, the HTTPsignature will be used and automatic event unmarshalling will be disabled.

For more details on this signature type, check out the Google Cloud Functionsdocumentation onbackground functions.

Enable CloudEvents

The Functions Framework can unmarshall incomingCloudEvents payloads to acloudevent object.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:

constfunctions=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 thedocs/ folder.

Contributing

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

About

FaaS (Function as a service) framework for writing portable Node.js functions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors48


[8]ページ先頭

©2009-2025 Movatter.jp