Code Monkey home page Code Monkey logo

faas-js-runtime's Introduction

FaaS Node.js Runtime Framework

This module provides a Node.js framework for executing a function that exists in a user provided directory path as an index.js file. The directory may also contain an optional package.json file which can be used to declare runtime dependencies for the function.

The function is loaded, and then invoked for incoming HTTP requests at localhost:8080. The incoming request may be a Cloud Event or just a simple HTTP GET request. In either case, the function will receive a Context object instance that has an event property. For a raw HTTP request, the incoming request is converted to a Cloud Event.

The invoked user function can be async but that is not required.

TBD: What format should the function response be? Should it also be a Cloud Event? But where is the event emitted? To some Knative Channel? Much is still to be determined.

Usage

In my current working directory, I have an index.js file like this.

const framework = require('faas-js-runtime');

// My function directory is in ./function-dir
framework(`${__dirname}/function-dir/`, server => {
  // The server is now listening on localhost:8080
  // and the function will be invoked for each HTTP
  // request to this endpoint.
  console.log('Server listening');

  // Whenever you want to shutdown the framework
  server.close();
});

In ./function-dir, there is an index.js file that looks like this.

module.exports = async function myFunction(context) {
  const ret = 'This is a test function for Node.js FaaS. Success.';
  return new Promise((resolve, reject) => {
    setTimeout(_ => {
      resolve(ret);
    }, 500);
  });
};

Sample

You can see this in action, executing the function at test/fixtures/async by running node hack/run.js.

Tests

Just run npm test.

faas-js-runtime's People

Contributors

lance avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.