Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Really basic express-like middleware implementation
class Middleware {
private middlewares = [];
push(...middlewares) {
this.middlewares.push.apply(this.middlewares, middlewares);
}
run(...args) {
let i = 0;
const next = () => {
if (i < this.middlewares.length) {
this.middlewares[i++].apply(null, [...args, next]);
}
}
next();
}
}
// test it out
let m = new Middleware();
m.push(
(ctx, next) => {
console.log(ctx);
ctx.a = 1;
next();
},
(ctx, next) => {
console.log(ctx);
ctx.a = 2;
next()
}
);
m.run({ a: 0 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.