Azure Middleware Engine
raw JSON → 1.0.1 verified Fri May 01 auth: no javascript
Azure Middleware Engine is a Node.js middleware engine for Azure Functions, inspired by Express, Fastify, and Hapi. Version 1.0.1 (stable) provides an intuitive API to apply middleware patterns in Azure Functions. It supports sequential middleware execution, conditional middleware via useIf, and validation. Alternatives like Azure Functions middleware libraries often lack chainable API or conditional execution. The package is lightweight with no external runtime dependencies, requiring Node >=8.9.4. It helps separate concerns like input parsing, validation, and error handling from business logic.
Common errors
error TypeError: MiddlewareHandler is not a constructor ↓
cause Importing the module incorrectly as default instead of named export.
fix
Use const { MiddlewareHandler } = require('azure-middleware');
error Error: next() called multiple times ↓
cause Calling next() more than once in a single middleware or after done().
fix
Ensure next() is called only once per middleware and not after context.done().
error Error: done already called ↓
cause Calling context.done() multiple times in the pipeline.
fix
Design pipeline so that only one middleware calls done() (typically the last).
Warnings
gotcha Missing next() call causes pipeline to hang. ↓
fix Always call context.next() in async or callback-style middlewares.
gotcha context.done() must be called exactly once; multiple calls may lead to race conditions. ↓
fix Ensure only one middleware calls context.done() and that it's called after all next() calls.
gotcha useIf predicate must return a boolean synchronously or via promise. ↓
fix Ensure predicate function returns a boolean or a promise resolving to boolean.
Install
npm install azure-middleware yarn add azure-middleware pnpm add azure-middleware Imports
- MiddlewareHandler wrong
const MiddlewareHandler = require('azure-middleware');correctconst { MiddlewareHandler } = require('azure-middleware'); - MiddlewareHandler (ESM) wrong
import MiddlewareHandler from 'azure-middleware';correctimport { MiddlewareHandler } from 'azure-middleware'; - azure-middleware (CommonJS entire module)
const azureMiddleware = require('azure-middleware');
Quickstart
const { MiddlewareHandler } = require('azure-middleware');
const myFunction = new MiddlewareHandler()
.use((context, next) => {
context.log.info('First middleware');
next();
})
.use((context, next) => {
context.log.info('Second middleware');
context.done(null, { body: 'Hello from Azure Function', status: 200 });
})
.listen();
module.exports = myFunction;