You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Invokes connect-like middleware if / unless routing criteria matches. Inspired on [express-unless](https://www.npmjs.com/package/express-unless) module.
3
-
>
1
+
# Introduction
2
+
Invokes connect-like middleware if / unless routing criteria match.
3
+
> Inspired by the [express-unless](https://www.npmjs.com/package/express-unless) module.
4
4
5
5
## Main features
6
6
- Advanced routes matching capabilities. Uses [find-my-way](https://www.npmjs.com/package/find-my-way) or any compatible router to match the routes.
7
-
-`iff`: execute middleware only if the routes matches. Ideal use case: API gateways (see: [fast-gateway](https://www.npmjs.com/package/fast-gateway))
8
-
-`unless`: execute middleware always unless the routes matches.
9
-
- Arbitraty chaining of iff -> unless of vice-versa.
10
-
- Low overhead, crazy fast implementation.
7
+
-`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))
8
+
-`unless`: execute middleware unless routing criteria is a match.
9
+
- Arbitraty chaining of `iff -> unless` of vice-versa.
Invokes connect-like middleware if / unless routing criteria match.
3
+
> Inspired by the [express-unless](https://www.npmjs.com/package/express-unless) module.
4
+
5
+
## Main features
6
+
- Advanced routes matching capabilities. Uses [find-my-way](https://www.npmjs.com/package/find-my-way) or any compatible router to match the routes.
7
+
-`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))
8
+
-`unless`: execute middleware unless routing criteria is a match.
9
+
- Arbitraty chaining of `iff -> unless` of vice-versa.
10
+
- Low overhead, blazing fast implementation.
11
+
12
+
# Usage
13
+
Install
14
+
```bash
15
+
npm i middleware-if-unless
16
+
```
17
+
18
+
Extending middleware
19
+
```js
20
+
constiu=require('middleware-if-unless')()
21
+
22
+
constmiddleware=function (req, res, next) {
23
+
res.body='hit'
24
+
25
+
returnnext()
26
+
}
27
+
28
+
// extend middleware with iff/unless capabilities
29
+
iu(middleware)
30
+
```
31
+
## unless
32
+
Execute middleware unless routing criteria is a match:
33
+
```js
34
+
constapp=require('express')()
35
+
app.use(middleware.unless([
36
+
'/not/allowed/to/hit'
37
+
]))
38
+
39
+
...
40
+
```
41
+
In this example, all requests except `[GET] /not/allowed/to/hit` will cause the middleware to be executed.
42
+
43
+
## iff
44
+
Execute middleware only if routing criteria is a match:
45
+
```js
46
+
constapp=require('express')()
47
+
app.use(middleware.iff([
48
+
{
49
+
methods: ['POST', 'DELETE', 'PUT', 'PATCH'],
50
+
url:'/tasks/:id'
51
+
}
52
+
]))
53
+
54
+
...
55
+
```
56
+
In this example, only a `[POST|DELETE|PUT|PATCH] /tasks/:id` request will cause the middleware to be executed.
57
+
# Chaining
58
+
You can optionally chain iff -> unless or vice-versa:
0 commit comments