Node.js 8 template for OpenFaaS with HTTP via Express.js
Clone or download
omrishtam and alexellis bump of-watchdog version to 0.4.6
Signed-off-by: Omri Shtamberger <omrishtam@gmail.com>
Latest commit 867c6c4 Dec 29, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
template bump of-watchdog version to 0.4.6 Dec 29, 2018
.dockerignore Initial Jan 24, 2018
.gitignore Add documentation and revised signature Feb 28, 2018
LICENSE Create LICENSE Oct 2, 2018
README.md Update README.md Nov 9, 2018

README.md

OpenFaaS Node.js 8 and Express.js template

This template provides additional context and control over the HTTP response from your function.

Status of the template

This template is pre-release and is likely to change - please provide feedback via https://github.com/openfaas/faas

The template makes use of the OpenFaaS incubator project of-watchdog.

See also: node10-express template

Supported platforms

  • x86_64 - node8-express
  • armhf - node8-express-armhf

Trying the template

$ faas template pull https://github.com/openfaas-incubator/node8-express-template
$ faas new --lang node8-express

Example usage

Example with success and JSON body:

"use strict"

module.exports = (event, context) => {
    let err;
    const result =             {
        status: "You said: " + JSON.stringify(event.body)
    };

    context.
        succeed(result);
}

Example of a custom HTTP status code:

"use strict"

module.exports = (event, context) => {
    let err;
    const result = {"message": "The record requested was not found."};

    context
        .status(404)
        .succeed(result);
}

Example of failure and plain-text body:

"use strict"

module.exports = (event, context) => {
    let err;
    const result = "Unable to process this event.";

    context
        .fail(result);
}

Example with use of optional callback parameter:

"use strict"

module.exports = (event, context, callback) => {
    let err;

    callback(err, {"result": "message received"});
}

Example with redirect (setting Location header):

"use strict"

module.exports = (event, context) => {
  context
    .headers({'Location': 'https://www.google.com/'})
    .status(307)    // Temporary
    .succeed('Page has moved.')
}

Other reference:

  • .status(code) - overrides the status code used by fail, or succeed
  • .fail(object) - returns a 500 error if .status(code) was not called prior to that
  • .succeed(object) - returns a 200 code if .status(code) was not called prior to that