https://github.com/BackendStack21/middleware-if-unless
Invokes connect-like middleware if / unless routing criteria matches. Inspired on express-unless module.
https://github.com/BackendStack21/middleware-if-unless
Last synced: about 1 month ago
JSON representation
Invokes connect-like middleware if / unless routing criteria matches. Inspired on express-unless module.
- Host: GitHub
- URL: https://github.com/BackendStack21/middleware-if-unless
- Owner: BackendStack21
- License: mit
- Created: 2019-01-03T20:49:35.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-01-17T16:35:33.000Z (about 1 year ago)
- Last Synced: 2024-05-20T03:19:52.192Z (11 months ago)
- Language: JavaScript
- Size: 124 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- cuban-opensource - middleware-if-unless - like middleware if / unless routing criteria matches. Inspired on express-unless module. (Web Development / Javascript)
README
# Introduction
[](https://www.npmjs.com/package/middleware-if-unless)
[](https://www.npmjs.com/package/middleware-if-unless)
[](https://www.npmjs.com/package/middleware-if-unless)
[](https://www.npmjs.com/package/middleware-if-unless)
[](https://github.com/BackendStack21/middleware-if-unless)
![]()
Invokes connect-like middleware if / unless routing criteria match.
> Inspired by the [express-unless](https://www.npmjs.com/package/express-unless) module. But a lot faster ;)## Main features
- Advanced routes matching capabilities. Uses [find-my-way](https://www.npmjs.com/package/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](https://www.npmjs.com/package/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
```bash
npm i middleware-if-unless
```Extending middleware
```js
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:
```js
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:
```js
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
- Website and documentation: https://iff-unless.21no.de