npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modofun

v1.2.2

Published

A fast function router for serverless Node.js applications with middleware support

Downloads

40

Readme

Features | Quick Start | Platforms | Configuration | Specification | Installation | Examples

Moderate fun with Modular Functions: a fast function router for serverless Node.js applications

var modofun = require('modofun')

exports.service = modofun({
  hello: (name) => 'Hi ' + name, // http://.../hello/John -> 200 OK 'Hi John'
  goodbye: () => 'See ya!'
})

Build Status Coverage Status npm

Why?

Modofun is a very lightweight Node.js package to help build nano/micro-services for serverless platforms (Google Cloud Functions, AWS Lambda, and Azure Functions). It aims to bridge the gap between too-small-single-function deployments, and more traditional Web/REST microservices. We want to group functions into modules.

Most serverless environments already provide a lot of facilities out of the box. And for these nanoservices, we really shouldn't be bothered with complex HTTP parsing. We should leverage HTTP, but in a more RPC kind of way.

Modofun is intentionally simplistic and small, and carries no dependencies. Which makes it a good choice for deploying small modules in serverless platforms.

Features

  • Routing to multiple functions
  • Parameter parsing
  • Automatic HTTP response building
  • Express/Connect middleware support
  • Multi-cloud:
    • Google Cloud Functions
    • AWS Lambda (with AWS API Gateway events)
    • Azure Functions
  • Support for ES6 Promises (or any other then-able)
  • Automatic error handling

Quick Start

A request is routed to a function based on the operation name, which is the first component of the application's path:

The remaining components of the path are added as arguments to the function.

Function Mode

This is the default mode. It makes it easy to expose an existing module as serverless cloud functions:

index.js

var modofun = require('modofun')
var myModule = require('./user-module')

// function mode enables argument expansion and handling of returned values
exports.user = modofun(myModule, { mode: 'function' })

user-module.js

async function get(username) { // async function with Promised return
  var user = await getFromDB(username)
  //...
  return user; // will respond 200 OK with JSON response body, or 404 Not Found if null
}
function setNickname(username, nickname) {
  //...
  return; // will respond 204 with no response body
}

Handlers can return a Promise, which means you can also use async/await.

Additional request data, like the request body, headers and query string, is also available in function mode as the this context when the handler function is called:

function update(username) { // e.g. POST /update/andy?force=1
  var updatedValues = this.body;
  var forceUpdate = this.query.force;
  //...
}

The complete list of fields available in the function context (this) can be found in the handlers specification.

An error response can be triggered by throwing an error, or returning a rejected Promise. If the error has a status field, the default error handler will use it to set the response status code accordingly. Otherwise it will respond with the 500 status code.

Request/Response Mode

It also works with traditional request/response handlers like those expected by Google Cloud Functions and Express:

var modofun = require('modofun')

var controller = {
  getUser: function(req, res) { // e.g. .../getUser/joe1976
    var [ username ] = req.params
    //...
    res.status(200).json(user)
  },
  //...
}

var app = modofun(controller, { mode: 'reqres' })

Platforms

Automatic Detection

You don't have to specify which platform your application will be deployed in. If no type is specified, modofun will automatically detect which platform it's running on by inspecting the environment variables set by the platform.

Google Cloud Functions

Applications of type gcloud create an event handler for Google Cloud Functions, but which also works with request/response frameworks like Express/Connect, etc.

You can force your application to return a handler of this type by setting the type option:

exports.handler = modofun(myModule, { type: 'gcloud' })

There is also a plugin to collect latency data (traces) from your application on Stackdriver: modofun-trace-agent-plugin.

AWS Lambda

Applications of type aws create a handler for AWS Lambda using API Gateway events. You can force your application to return a handler of this type by setting the type option:

exports.handler = modofun(myModule, { type: 'aws' })

Azure Functions

Applications of type azure create a handler for Azure Functions using an HTTP trigger. You can force your application to return a handler of this type by setting the type option:

exports.handler = modofun(myModule, { type: 'azure' })

Configuration

Enhance with middleware and custom error handlers:

var modofun = require('modofun')
//...

exports.app = modofun(
  {
    authenticate: authenticate,
    user: [ authorize, getUser ] // auth middleware preceding specific operations
  },
  {
    middleware: [ logger ], // global middleware that runs every time
    errorHandler: (err, req, res) => res.status(500).send(err.message) // custom error handler
  }
)

The error handler takes care of catching both rejected promises and thrown Errors. There is a default error handler that should be sufficient for most cases.

Middleware

Apply commonly used middleware:

var modofun = require('modofun')
var morgan = require('morgan')
var cors = require('cors')
var jwt = require('express-jwt')
var controller = require('./service-controller')

exports.service = modofun(controller, [ morgan('tiny'), cors(), jwt(secret) ])

Function Arity

When in function mode, all functions are automatically checked for the correct number of parameters according to the handler's function arity, but this behavior can be disabled through the checkArity option.

You can also enforce the correct number of input arguments for your functions by using the included arity checker middleware (e.g. in reqres mode, or when you want to selectively enforce arity checks per function):

var modofun = require('modofun')
//...

const app = modofun(
  {
    authenticate: authenticate, // no arity validation, just needs to start with /authenticate/
    get: [ modofun.arity(1), getUser ], // /get/jdoe
    updatePIN: [ authorize, modofun.arity(2), updatePIN ] // /updatePIN/jdoe/9876
  }
)

exports.user = app

Which generates a 400 error if the request doesn't match the expected function arity.

Note that, in Javascript, a function's arity is only counted up to the first optional argument (i.e. the first argument with a default value). As an example of this, the following function is considered to have an arity of 2:

function foo(one, two, three='optional', four) {
  //...
}

Specification

API

modofun(handlers)

Creates an application with default options.

  • handlers: An object with functions named after the operations to be exposed via the URL path. See the handlers specification below.

modofun(handlers, middleware)

Creates an application with a list of global middleware to execute before the invoking the route handlers.

  • middleware: An array of Connect-style middleware.

modofun(handlers, options)

Create an application with an options object.

modofun.arity(minimum)

Enforces a minimum amount of arguments for functions. Can be applied as an operation specific middleware, or even as a global middleware (if all your functions happen to have the same arity).

  • minimum: An integer number.

modofun.arity(minimum, maximum)

Same as above, except that the additional maximum adds an upper limit for the allowed amount of arguments for functions.

  • minimum: An integer number.
  • maximum: An integer number.

Handlers

The object describing the operations to be exposed by the application. You can also specify operation specific middleware by passing an array instead of a function.

{
  // Handler in function mode
  // Path parameters are passed as the function arguments.
  // Additional request data is added to the function's context as the *this*.
  operationA: (param_0[, param_N]) => { return 'Hello' },

  // Handler in reqres mode
  // req.params is filled with the path parameters (function arguments)
  // that follow the operation name.
  operationB: (req, res) => { res.send('Hello') },

  // Operation with preceding middleware
  // The last element of the array should be the handler for the operation.
  // This works in both function and http modes.
  operationC: [ ...middleware, () => { /* Do stuff */ } ]
}

Request Data in Function Mode

Handlers in function mode can find additional request data in the function's context (this), which contains the following properties:

  • this.method: string for the HTTP request method
  • this.headers: object with HTTP request headers
  • this.body: object with the parsed JSON body
  • this.query: object with the URL query parameters
  • this.user: object commonly used to store authenticated user information (e.g. Passport, JWT, etc)

Options

Here is a list of the available options and their default values:

{
  type, // possible values are: 'gcloud', 'aws' or 'azure', otherwise auto detection kicks in
  mode: 'function', // possible values are: 'function' or 'reqres'
  middleware: [], // middleware is executed according to the order in the array
  checkArity: true, // possible values are: true and false
  errorHandler: modofun.defaultErrorHandler // function(error, request, response)
}

AWS and Azure Request/Response Wrappers

Modofun creates request and response wrappers for AWS and Azure, to be used by standard Connect/Express middleware, and by handlers in reqres mode. However, these are not complete request/response objects as you'd find in Express or vanilla Node.js HTTP servers. They're limited to the most common data/methods required for cloud functions, and that map best to AWS and Azure events. Here is an overview:

Request = {
  method,
  path,
  query,
  headers,
  body,
  get: (name) => {},    // request header getter
  header: (name) => {}, // alias of get()
  // extra only for AWS:
  awsEvent,  // the original event object sent by AWS Lambda
  awsContext, // the original context object sent by AWS Lambda
  // and extra only for Azure:
  azureRequest // the original request object sent by Azure
}

Response = {
  setHeader: (name, value) => {},
  status: (code) => {},
  send: (body) => {},
  json: (body) => {},
  end: () => {}
}

Note: Because of this, some middleware may not work properly when running in AWS Lambda or Azure. If you find middleware that you'd like to use, but that is not compatible with Modofun, please report it on GitHub.

Installation

You can install it using npm:

$ npm install modofun

Or yarn:

$ yarn add modofun

Examples

The best way to start is to look through the examples.