Files
fission-src/examples/nodejs/README.md
T
rapitableandSoam Vasani 5c47073518 Make default node-env use alpine. List envs in documentation. (#354)
closes #319 

* Make default node-env use alpine, and support separate debian env. List envs in documentation.

* Copy ONBUILD base image commands into node Dockerfiles

* Copy more commands from onbuild image

* Tidy dockerfiles
2017-10-04 19:05:43 -07:00

3.4 KiB
Raw Blame History

Fission Node.js Examples

This directory contains several examples to get you started using Node.js with Fission.

Environment

Before running any of these functions, make sure you have created a nodejs Fission environment:

$ fission env create --name nodejs --image fission/node-env

Note: The default fission/node-env image is based on Alpine, which is much smaller than the main Debian Node image (65MB vs 680MB) while still being suitable for most use cases. If you need to use the full Debian image use the fission/node-env-debian image instead. See the official Node docker hub repo for considerations relating to this choice.

Function signature

Every Node.js function has the same basic form:

module.exports = async function(context) {
    return {
        status: 200,
        body: 'Your body here',
        headers: {
            'Foo': 'Bar'
        }
    }    
}

Since it is an async function, you can await Promises, as demonstrated in the stock.js function.

hello.js

This is a basic "Hello, World!" example. It simply returns a status of 200 and text body.

Usage

# Upload your function code to fission
$ fission function create --name hello --env nodejs --code hello.js

# Map GET /hello to your new function
$ fission route create --method GET --url /hello --function hello

# Run the function.
$ curl http://$FISSION_ROUTER/hello
Hello, world!

hello-callback.js

This is a basic "Hello, World!" example implemented with the legacy callback implementation. If you declare your function with two arguments (context, callback), a callback taking three arguments (status, body, headers) is provided.

⚠️ Callback support is only provided for backwards compatibility! We recommend that you use async functions instead.

Usage

# Upload your function code to fission
$ fission function create --name hello-callback --env nodejs --code hello-callback.js

# Map GET /hello-callback to your new function
$ fission route create --method GET --url /hello-callback --function hello-callback

# Run the function.
$ curl http://$FISSION_ROUTER/hello-callback
Hello, world!

stock.js

This is a basic example of how you can easily use asynchronous requests in your functions. By default, the Node.js environment makes the request-promise-native library available. In this example, the Google Finance API is used to determine when a stock was last traded.

Usage

# Upload your function code to fission
$ fission function create --name stock --env nodejs --code stock.js

# Map GET /stock to your new function
$ fission route create --method GET --url /stock --function stock

# Run the function.
$ curl -H "Content-Type: application/json" -X POST -d '{"symbol":"AAPL"}' http://$FISSION_ROUTER/stock

{"text":"AAPL last traded at 138.99"}

kubeEventsSlack.js

This example watches Kubernetes events and sends them to a Slack channel. To use this, create an incoming webhook for your Slack channel, and replace the slackWebhookPath in the example code.

Usage

# Upload your function code to fission
$ fission fn create --name kubeEventsSlack --env nodejs --code kubeEventsSlack.js

# Watch all services in the default namespace:
$ fission watch create --function kubeEventsSlack --type service --ns default