Generic compose
Hi @blakeembrey
During the implementation, I noticed that Express app.use and app[METHOD] accepts variadic number of middlewares implementing internally the composition as @knksmith57 expected in #4
However having this implementation is useful for having same behaviour as express in other server libraries like connect.
Cheers!
Coverage increased (+1.4%) to 94.097% when pulling c1743535fd3ab42baecc8d488e227df7036d045a on xgbuils:generic-compose into a379394f76deb213a631bbc0a7e195c2ea5b3908 on blakeembrey:master.
Coverage increased (+1.4%) to 94.097% when pulling c1743535fd3ab42baecc8d488e227df7036d045a on xgbuils:generic-compose into a379394f76deb213a631bbc0a7e195c2ea5b3908 on blakeembrey:master.
@blakeembrey
Another quirky feature that's may be semi-useful is that arrays are mutable whereas arguments are not -this could be a reason to accept an array instead of an arguments list in cases where someone may want to mutate middleware in the future.
Could you give a use case about mutating the array and then using it?
Thanks!
Could you give a use case about mutating the array and then using it?
const arr = []
const handler = compose(arr)
// Some time later...
arr.pop()
arr.push(() => 'blah')
Not saying it's a particularly good practice, but it is something possible we support an array only.
Hi @blakeembrey
I was asking about a real use case because I think we don't need to implement solution that no one is using. For example I'm tried to implement a case where the words of the response rotates after each request trying to use array mutation:
First response after first request:
this is a mutable example
Second response after second request:
is a mutable example this
Third response after third request:
a mutable example this is
However, I was not able to get a solution with the current versions of express and compose-middleware libraries:
const express = require('express');
const app = express();
const { compose } = require('compose-middleware');
const middlewares = [
(req, res, next) => {
res.write('this ');
next();
},
(req, res, next) => {
res.write('is ');
next();
},
(req, res, next) => {
res.write('a ');
next();
},
(req, res, next) => {
res.write('mutable ');
next();
},
(req, res, next) => {
res.write('example ');
next();
}
];
app.get('/', compose(middlewares));
app.use('/', (req, res, next) => {
middlewares.push(middlewares.shift());
console.log(middlewares.map(f => f.toString()))
console.log('\n')
res.end()
next();
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Then, do you have a real example where array mutation is applicable and useful?
Thanks!