Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

middleware-if-unless

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

middleware-if-unless

Invokes connect-like middleware if / unless routing criteria matches. Inspired on express-unless module.

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1K
decreased by-21.73%
Maintainers
1
Weekly downloads
 
Created
Source

Introduction

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

Keywords

FAQs

Package last updated on 24 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc