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 🙏

© 2025 – Pkg Stats / Ryan Hefner

azure-func-middleware

v1.0.0

Published

A middleware cascade for Azure Functions

Downloads

2

Readme

azure-func-middleware

Build Status

A middleware cascade implementation for Azure Functions JS 2.x (inspired by Koa and Express).

Install

npm install azure-func-middleware --save

Usage

Main flow

Method use adds middleware handler to a cascade. Middleware handlers are executed in the order they are added. To go to the next middleware handler, use the next callback. Method listen composes middlewares to the Azure Function handler.

const AzureFuncMiddleware = require('azure-func-middleware');

module.exports = new AzureFuncMiddleware()
  .use(async (ctx, next) => {
    // will be called first
    // ...
    next();
  })
  .use((ctx, next) => {
    // will be called second if no error in first
    // ...
    ctx.done(null, { status: 200 });
  })
  .catch((err, ctx, next) => {
    // will be called if there is error in first or second
    // ...
    ctx.done(null, { status: 500 });
  })
  .listen();

Response

To complete the response process, use done callback of the context object

Using with the $return output binding

function.json

{
  "bindings": [
    ...
    {
    "type": "http",
    "direction": "out",
    "name": "$return"
    }
  ]
}

index.js

const AzureFuncMiddleware = require('azure-func-middleware');

module.exports = new AzureFuncMiddleware()
  .use((ctx) => {
    const response = {
      status: 200,
      body: 'OK'
    };
    ctx.done(null, response);
  })
  .listen();

Using with the named output binding

function.json

{
  "bindings": [
    ...
    {
    "type": "http",
    "direction": "out",
    "name": "res"
    }
  ]
}

index.js

const AzureFuncMiddleware = require('azure-func-middleware');

module.exports = new AzureFuncMiddleware()
  .use((ctx) => {
    ctx.res = {
      status: 200,
      body: 'OK'
    };
    ctx.done();
  })
  .listen();

Capturing errors

If an error is thrown in the middleware handler, then the nearest error middleware handler is called. Error handlers are added by the catch method.

const AzureFuncMiddleware = require('azure-func-middleware');

module.exports = new AzureFuncMiddleware()
  .use((ctx, next) => {
    throw new Error('Error!'); // or next(new Error('Error!'));
  })
  .use(async (ctx, next) => {
    // won't be called
    // ...
    next()
  })
  .catch((err, ctx, next) => {
    // will be called
    ctx.done(null, {
      status: 500,
      body: err.message // 'Error!' 
    });
  })
  .listen();

Passing data through middlewares

For passing data through middlewares you can use namespace state of the context object

const AzureFuncMiddleware = require('azure-func-middleware');

module.exports = new AzureFuncMiddleware()
  .use(async (ctx, next) => {
    ctx.state.count = 1;
    next();
  })
  .use((ctx, next) => {
    ctx.state.count += 1; // ctx.state.count === 2
    ctx.done(null, { status: 200 });
  })
  .listen();

Conditional middlewares

The method useIf adds a middleware handler that will be executed if the predicate returns true.

const AzureFuncMiddleware = require('azure-func-middleware');

module.exports = new AzureFuncMiddleware()
  .useIf(ctx => ctx.req.method === 'HEAD', (ctx, next) => {
    // will be called if HEAD request 
    // ...
  })
  .useIf(ctx => ctx.req.method === 'GET', (ctx, next) => {
    // will be called if GET request 
    // ...
  })
  .listen();

Common middlewares

Often Azure Functions use a common sequence of middlewares. You can declare this sequence and add using the method useMany.

common.js

const defineUser = (ctx, next) => { 
  //... 
};

const checkRoles = (ctx, next) => {
  //... 
};

module.exports = [
  defineUser,
  checkRoles
]

index.js

const AzureFuncMiddleware = require('azure-func-middleware');
const common = require('common');


module.exports = new AzureFuncMiddleware()
  .useMany(common)
  .use((ctx, next) => {
      // will be called after common
    })
  .listen();

Testing

The Azure Function handler returns a promise. This fact can be used for testing.

Using with the $return output binding

function.json

{
  "bindings": [
    ...
    {
    "type": "http",
    "direction": "out",
    "name": "$return"
    }
  ]
}

test.js

const funcHandler = require(...);

it('should work', async () => {
  const context = { ... };
  const expectedBody = ...;
  const { status, body } = await funcHandler(context);
  expect(status).toEqual(200);
  expect(body).toEqual(expectedBody);
});

Using with the named output binding

function.json

{
  "bindings": [
    ...
    {
    "type": "http",
    "direction": "out",
    "name": "res"
    }
  ]
}

test.js

const funcHandler = require(...);

it('should work', async () => {
  const context = { ... };
  const expectedBody = ...;
  await funcHandler(context);
  expect(context.res.status).toEqual(200);
  expect(context.res.body).toEqual(expectedBody);
});

API

Table of Contents

AzureFuncMiddleware

Parameters

  • options Object (optional, default {})
    • options.silent boolean (optional, default false)

use

Add a middleware to a cascade

Parameters

useIf

Add a middleware with condition to a cascade

Parameters

useMany

Add several middlewares to a cascade

Parameters

useManyIf

Add several middlewares to a cascade with condition

Parameters

catch

Add a middleware for error handling to a cascade

Parameters

catchIf

Add a middleware for error handling with condition to a cascade

Parameters

listen

Compose middlewares to a function handler

Returns funcHandler

funcHandler

Type: Function

Parameters

Returns Promise

middlewareHandler

Type: Function

Parameters

errMiddlewareHandler

Type: Function

Parameters

next

Type: Function

Parameters

predicate

Type: Function

Parameters

Returns boolean