Functions Framework

The Functions Framework lets you write lightweight functions that run in many different environments, including:

For example, in Node.js 10, the framework allows you to go from:

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.

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

Quickstart

Create an index.js file with the following contents:

exports.helloWorld = (req, res) => {
  res.send('Hello, World');
};

To run a function locally, first create a package.json file using npm init:

npm init

Now install the Functions Framework:

npm install @google-cloud/functions-framework

Add a start script to package.json, with configuration passed via command-line arguments:

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

Use npm start to start the built-in local development server:

npm start
...
Serving function...
Function: helloWorld
URL: http://localhost:8080/

Send requests to this function using curl from another terminal window:

curl localhost:8080
# Output: Hello, World

Learn More

You can find more detailed documentation on how to use the Functions Framework on the GitHub page.

Оцените, насколько информация на этой странице была вам полезна:

Оставить отзыв о...

Текущей странице
Cloud Functions Documentation