Switch branches/tags
0.2.1-versionupdates FunctionVersioning VersioningProposal add-python-test add-upgrade-guide-from-0.4-to-0.5 add-version-to-binary annotations_proposal async-requests autoscaling build-and-push-logger builder-builds builder-image bump-nats-limits cache_obj_type checkCRDcreationError cliAutoComplete close-idle-connectio common-version controller-tpr-compatible-types controller-tpr debug-only deprecate-fn-pods docs-provider-docker env-args-ci-test errorMessageForFailedCanary execution_strategy executor_abstraction extensions_proposal fetcher_retry fission-spec fission-workflows-0.2.0 fix-932 fix-998 fix-binary-environment-build-failure fix-components-crash-before-crds-creation fix-executor-cleanup-process fix-fluentd fix-function-not-update-problem fix-log-ordering fix-nd fix-post-install-job-failure fix-secret-cfgmap-update-problem fix-test_env_vars-failure fix_executor_panic fixFissionCore fluentd-configmap fluentd-workflows-2 fn_pods_removal gcpug-demo gevent-environment-ci-build glide_up_ci go-build-helper go-build-script-example go-builder go-env-fixes-457 golang-image-versions handle-multiple-http-methods hook-bugfixes idle-conn-timeout install-instructions-use-gh-releases issue/37 istio-integration java_env_design jvm-env-prototype jvm-specs jvm_heap jvm_issue jvm_readme k6-benchmark kafka_tests mac_tests master migrate-donet-project-json nd_pkg_update_issue-taching newdeploy_backend nodejsv2-test poolsize_env_fix pr371-copy prithvi-logging-branch promethus-fission-metrics pull-image-from-private-registry py-flask-bug python-env-variants record refactor-roundtrip resource-version-monitor retry-for-network-error router_caching_podip secret-cfgmap-branch secret-cfgmap-doc set-idle-conn-timeout show-warning-when-creating-route-with-non-existent-function skaffold_dev_workflow spec-flags spec_helm_removal swagger-api-spec temp_buildtime_reduction temp_test_disable test-connect test-logging-timestamps test_failure_investigation test_improvement tracing-places update-docs-problem update-go-env-version upgrade_test_ci upstream/fix-test_env_vars-failure use-separate-controller-loop-to-watch-functions-changes v0.5.0-changelog-patch-1 v0.5.0 v0.6.0 v0.6.1 v0.7.0 v0.7.1 v0.8.0 v0.9.0 v0.9.1 v0.11.0 v0.12.0 v2-types-and-tpr wait-for-components-before-testing wait-for-pods-before-testing windows-instructions xiekeyang/pr713
Nothing to show
Find file History

README.md

Fission Node.js Examples

This is V2 example, check here for V1.

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

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

# Create an environment with default nodejs images
$ fission env create --name nodeenv --image fission/node-env:latest --builder fission/node-builder:latest
# Create zip file from our example
$ zip -jr nodejs.zip nodejs/
# Create a package with the zip file
$ fission pkg create --sourcearchive nodejs.zip --env nodeenv

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'
        }
    }
}

hello.js

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

Usage

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

# Create a function
$ fission fn create --name hello --pkg [pkgname] --entrypoint "hello"

# Test the function
$ fission fn test --name hello

index.js

This file does nothing but for demonstrating require feature.

Usage

# Create a function, you can skip `--entrypoint` as node will look for `index.js` by default
$ fission fn create --name index --pkg [pkgname]

# Test the function
$ fission fn test --name index

multi-entry.js

This is a multiple exports example. There are two exports: entry1 and entry2

Usage

# Create a function for entry1
$ fission fn create --name entry1 --pkg [pkgname]  --entrypoint "multi-entry.entry1"

# Test the function
$ fission fn test --name entry1

# Create a function for entry2
$ fission fn create --name entry2 --pkg [pkgname]  --entrypoint "multi-entry.entry2"

# Test the function
$ fission fn test --name entry2

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

# Create a function
$ fission fn create --name hello-callback --pkg [pkgname] --entrypoint "hello-callback"

# 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!

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 --pkg [pkgname] --entrypoint "hello-callback"

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

weather.js

In this example, the Yahoo Weather API is used to current weather at a given location.

Usage

# Upload your function code to fission
$ fission function create --name weather --pkg [pkgname] --entrypoint "weather"

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

# Run the function.
$ curl -H "Content-Type: application/json" -X POST -d '{"location":"Sieteiglesias, Spain"}' http://$FISSION_ROUTER/weather

{"text":"It is 2 celsius degrees in Sieteiglesias, Spain and Mostly Clear"}