The Functions Framework lets you write lightweight functions that run in OpenFunction, supports different kinds of function signature type. All you need to do is providing a function and specify a few params.
-
Install the functions framework:
$ npm install -g @openfunction/functions-framework
-
Create an
index.js
file with the following contents:exports.helloWorld = (data) => { return 'Hello, World'; };
-
Run the following command:
$ npx @openfunction/functions-framework --target=helloWorld --source openfunction
-
Open
http://localhost:8080/
in your browser and see{"data": "Hello, World"}
.
-
Create a
package.json
file usingnpm init
:$ npm init
-
Create an
index.js
file with the following contents:exports.helloWorld = (data) => { return 'Hello, World'; };
-
Now install the Functions Framework:
$ npm install @openfunction/functions-framework
-
Add a
start
script topackage.json
, with configuration passed via command-line arguments:"scripts": { "start": "functions-framework --target=helloWorld --source openfunction" }
-
Use
npm start
to start the built-in local development server:$ npm start ... The functionModulePath is: /.../.../index.js Openfunction functions framework listening at http://localhost:8080
-
Send requests to this function using
curl
from another terminal window:$ curl localhost:8080 # Output: {"data": "Hello, World"}
-
Create a
package.json
file usingnpm init
:$ npm init
-
Create an
index.js
file with the following contents:exports.helloWorld = (req, res) => { res.send('Hello, World'); };
-
Now install the Functions Framework:
$ npm install @openfunction/functions-framework
-
Add a
start
script topackage.json
, with configuration passed via command-line arguments:"scripts": { "start": "functions-framework --target=helloWorld --source openfunction http" }
-
Use
npm start
to start the built-in local development server:$ npm start ... The functionModulePath is: /.../.../index.js Openfunction functions framework listening at http://localhost:8080
-
Send requests to this function using
curl
from another terminal window:$ curl localhost:8080 # Output: Hello, World
By default, functions framework see the incoming request as a Openfunction request, so the simplest function can be like this:
/**
* Send {"data": "Hello, World"}
* @param data - data is the request body contents, which should be a json object
*/
exports.helloWorld = (data) => {
return 'Hello, World';
};
Function framework support the following type of function source:
- OpenFunction (OpenFunction native type)
- HTTP
- CloudEvent
Here are some examples to give you a quick view of functions definition in different function source type.
You can configure the Functions Framework using command-line flags.
Flag | Required | Description | |
---|---|---|---|
-p, --port |
False | The port on which the Functions Framework listens for requests. Default: 8080 |
|
-t, --target |
True | The name of the exported function to be invoked in response to requests. | |
-s, --source |
False | Function source type. Default: http ; accepted values: http or or cloudevent or openfunction |
You can set command-line flags in your package.json
via the start
script. For example:
"scripts": {
"start": "functions-framework --target=helloWorld --source cloudevent"
}