https://github.com/dev-force/express-conditional-tree-middleware
Create a tree of middleware nodes that may either be middlewares or combination of middlewares in ExpressJS
https://github.com/dev-force/express-conditional-tree-middleware
async conditional expressjs middleware middleware-nodes tree
Last synced: 9 days ago
JSON representation
Create a tree of middleware nodes that may either be middlewares or combination of middlewares in ExpressJS
- Host: GitHub
- URL: https://github.com/dev-force/express-conditional-tree-middleware
- Owner: Dev-Force
- License: mit
- Created: 2016-11-20T20:01:50.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-12T09:35:36.000Z (about 8 years ago)
- Last Synced: 2025-10-14T14:17:42.170Z (5 months ago)
- Topics: async, conditional, expressjs, middleware, middleware-nodes, tree
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/express-conditional-tree-middleware
- Size: 6.84 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Express Conditional Tree Middleware
Hi, this tool was made in order for me to be able to combine different and asynchronous middleware
using different conditions but also combine those conditions with other middleware and other conditions.
I am dedicated to create scalable and easy to use tools, so this is one of them and I thought it would be nice if I could share it with you.
## TL;DR
Create a tree of middleware nodes that may either be middlewares or combination of middlewares.
Note: The tree is parsed in a DFS fashion.
Important Note: And Chainers are executed sequentially. OrChainers are using the Promise.any and as a result they are executed in parallel. I want to implement them soon. If anyone wants to contribute please go ahead :)
### Usage Example
```javascript
import { conditionalMiddleware, MiddlewareChainer, OrMiddlewareStrategy, AndMiddlewareStrategy } from 'express-conditional-tree-middleware';
// Every Node class must have a applyMiddleware method in order to be used by a chainer
class IPLimitMiddlewareNode {
applyMiddleware(app) {
return (req, res, next) => {
return new Promise((resolve, reject) => {
if(app.get('myIPs').indexOf(req.ip) > -1) return resolve();
else reject();
});
};
}
}
class AdminMiddlewareNode {
applyMiddleware(app) {
return (req, res, next) => {
return new Promise((resolve, reject) => {
if(req.body.admin === true) return resolve();
else reject();
});
};
}
}
class TokenVerificationMiddlewareNode {
applyMiddleware(app) {
return (req, res, next) => {
return new Promise((resolve, reject) => {
if(token.verify()) return resolve();
reject();
});
}
}
}
let myRouter = express.Router();
// Create two new strategies
let orChainer = new MiddlewareChainer(new OrMiddlewareStrategy());
let andChainer = new MiddlewareChainer(new AndMiddlewareStrategy());
// Create the tree
andChainer.add(new TokenVerificationMiddlewareNode());
andChainer.add(new AdminMiddlewareNode());
orChainer.add(andChainer);
orChainer.add(new IPLimitMiddlewareNode());
//
// Tree Structure
//
// Or
// / \
// / \
// And IpLimit
// / \
// / \
// TokenVerification Admin
//
myRouter.use(
// conditionalMiddleware functions take as arguments the express app, a router that will apply
// the middleware tree, the root element of the tree and a callback error function
conditionalMiddleware(app, myRouter, orChainer, function(res) {
res.status(401).json({status: 'Unauthorized'});
})
)
```
### Design Patterns Used
* Strategy
* Composite
### License
MIT