1.4.0 • Published 1 year ago

middleware-if-unless v1.4.0

Weekly downloads
960
License
MIT
Repository
github
Last release
1 year ago

Introduction

NPM version NPM Total Downloads License TypeScript support Github stars

Invokes connect-like middleware if / unless routing criteria match.

Inspired by the express-unless module.

Main features

  • Advanced routes matching capabilities. Uses find-my-way or any compatible router to match the routes.
  • iff: execute middleware only if routing criteria is a match. Ideal use case: API gateways (see: fast-gateway)
  • unless: execute middleware unless routing criteria is a match.
  • Arbitraty chaining of iff -> unless of vice-versa.
  • Low overhead, blazing fast implementation.

Usage

Install

npm i middleware-if-unless

Extending middleware

const iu = require('middleware-if-unless')()

const middleware = function (req, res, next) {
  res.body = 'hit'

  return next()
}

// extend middleware with iff/unless capabilities
iu(middleware)

unless

Execute middleware unless routing criteria is a match:

const app = require('express')()
app.use(middleware.unless([
  '/not/allowed/to/hit'
]))

...

In this example, all requests except [GET] /not/allowed/to/hit will cause the middleware to be executed.

iff

Execute middleware only if routing criteria is a match:

const app = require('express')()
app.use(middleware.iff([
  {
    methods: ['POST', 'DELETE', 'PUT', 'PATCH'],
    url: '/tasks/:id'
  }
]))

...

In this example, only a [POST|DELETE|PUT|PATCH] /tasks/:id request will cause the middleware to be executed.

More

1.4.0

1 year ago

1.3.0

2 years ago

1.2.5

3 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago