All Projects → openfaas-incubator → node10-express-template

openfaas-incubator / node10-express-template

Licence: MIT License
Node.js 10 Express Template for OpenFaaS

Programming Languages

javascript
184084 projects - #8 most used programming language
Dockerfile
14818 projects

Projects that are alternatives of or similar to node10-express-template

Of Watchdog
Reverse proxy for STDIO and HTTP microservices
Stars: ✭ 175 (+573.08%)
Mutual labels:  lambda, faas
2020
Make your own 2020 ASCII art
Stars: ✭ 26 (+0%)
Mutual labels:  faas, openfaas
Components
The Serverless Framework's new infrastructure provisioning technology — Build, compose, & deploy serverless apps in seconds...
Stars: ✭ 2,259 (+8588.46%)
Mutual labels:  lambda, faas
Flogo
Project Flogo is an open source ecosystem of opinionated event-driven capabilities to simplify building efficient & modern serverless functions, microservices & edge apps.
Stars: ✭ 1,891 (+7173.08%)
Mutual labels:  lambda, faas
faas-fargate
OpenFaaS on AWS Fargate. Open source Functions as a Service without any infrastructure to manage
Stars: ✭ 50 (+92.31%)
Mutual labels:  faas, openfaas
Refunc
A lib make building AWS Lambda compatible layer easily
Stars: ✭ 144 (+453.85%)
Mutual labels:  lambda, faas
vcenter-connector
Extend vCenter with OpenFaaS
Stars: ✭ 29 (+11.54%)
Mutual labels:  faas, openfaas
Kotless
Kotlin Serverless Framework
Stars: ✭ 721 (+2673.08%)
Mutual labels:  lambda, faas
lambda-memory-performance-benchmark
Performance and cost benchmark tool for AWS Lambda on memory sizes 📈⏱
Stars: ✭ 60 (+130.77%)
Mutual labels:  lambda, faas
leaderboard-app
GitHub leaderboard for your organisation or repo (Serverless SPA)
Stars: ✭ 64 (+146.15%)
Mutual labels:  lambda, openfaas
Gofn
Function process via docker provider (serverless minimalist)
Stars: ✭ 134 (+415.38%)
Mutual labels:  lambda, faas
openfaas-rstats-templates
OpenFaaS templates for R
Stars: ✭ 17 (-34.62%)
Mutual labels:  faas, openfaas
Je
A distributed job execution engine for the execution of batch jobs, workflows, remediations and more.
Stars: ✭ 30 (+15.38%)
Mutual labels:  lambda, faas
Laravel Bridge
Package to use Laravel on AWS Lambda with Bref
Stars: ✭ 168 (+546.15%)
Mutual labels:  lambda, faas
Lambdalogs
A CLI tool to trace AWS Lambda calls over multiple CloudWatch log groups.
Stars: ✭ 18 (-30.77%)
Mutual labels:  lambda, faas
Bref
Serverless PHP on AWS Lambda
Stars: ✭ 2,382 (+9061.54%)
Mutual labels:  lambda, faas
Functional Typescript
TypeScript standard for rock solid serverless functions.
Stars: ✭ 600 (+2207.69%)
Mutual labels:  lambda, faas
Faas Cli
Official CLI for OpenFaaS
Stars: ✭ 633 (+2334.62%)
Mutual labels:  lambda, faas
node8-express-template
Node.js 8 template for OpenFaaS with HTTP via Express.js
Stars: ✭ 16 (-38.46%)
Mutual labels:  faas, openfaas
go-appsync-graphql-cloudformation
AWS AppSync GraphQL API Proxy with Lambda, CloudFormation, and SAM
Stars: ✭ 28 (+7.69%)
Mutual labels:  lambda, faas

OpenFaaS Node.js 10 (LTS) 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.

Supported platforms

  • x86_64 - node10-express and node10-express-service
  • armhf - node10-express-armhf

Trying the template

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

Example usage - node10-express, node10-express-arm64, node10-express-armhf

Success and JSON body

"use strict"

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

    context.
        succeed(result);
}

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);
}

Failure code and plain-text body:

"use strict"

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

    context
        .fail(result);
}

Using the optional callback parameter:

"use strict"

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

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

Redirect (setting Location header):

"use strict"

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

Path-based routing (multiple-handlers):

"use strict"

module.exports = (event, context) => {
  if(event.path == "/login") {
      return login(event, context);
  }

  return context
        .status(200)
        .succeed('Welcome to the homepage.')
}

function login(event, context) {
    return context
        .status(200)
        .succeed('Please log in.')
}

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

Example usage - node10-express-service

This template provides Node.js 10 (LTS) and full access to express.js for building microservices for OpenFaaS, Docker, Knative and Cloud Run.

With this template you can create a new microservice and deploy it to a platform like OpenFaaS for:

  • scale-to-zero
  • horizontal scale-out
  • metrics & logs
  • automated health-checks
  • sane Kubernetes defaults like running as a non-root user

Minimal example with one route

"use strict"

module.exports = async (config) => {
    const app = config.app;

    app.get('/', (req, res) => {
        res.send("Hello world");
    });
}

Minimal example with one route and npm package

npm install --save moment
"use strict"

const moment = require('moment');

module.exports = async (config) => {
    const app = config.app;

    app.get('/', (req, res) => {
        res.send(moment());
    });
}

Example usage with multiple routes, middleware and ES6

"use strict"

module.exports = async (config) => {
    const routing = new Routing(config.app);
    routing.configure();
    routing.bind(routing.handle);
}

class Routing {
    constructor(app) {
        this.app = app;
    }

    configure() {
        const bodyParser = require('body-parser')
        this.app.use(bodyParser.json());
        this.app.use(bodyParser.raw());
        this.app.use(bodyParser.text({ type : "text/*" }));
    }

    bind(route) {
        this.app.post('/*', route);
        this.app.get('/*', route);
        this.app.patch('/*', route);
        this.app.put('/*', route);
        this.app.delete('/*', route);
    }

    handle(req, res) {
        res.send(JSON.stringify(req.body));
    }
}

handler.js

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].