Avoid multiple redirections when resolving seo friendly urls.
- modular resolutions through standard middleware - named washing machines (WM)
- mimic standard middleware usage
- expose a single middleware (possibly used as filter, configurable)
Replace the following redirections performed by several middleware functions :
Incoming
http://www.stu.ff/cn/?noise=1
removeSubdomain > 303 http://stu.ff/cn/?noise=1
setDefaultLanguage > 307 http://stu.ff/en/?noise=1
removeQueryParams > 301 http://stu.ff/en/
by a single redirection keeping the same middleware functions with laundromat.
Incoming
http://www.stu.ff/cn/?noise=1
laundromat > 301 http://stu.ff/en/
Configure middleware by adding washing machines functions :
var Laundromat = require('./lib');
// ...
var laundromat = new Laundromat();
// Set laundromat _washing machines_ (that are actually standard middleware).
//
// On last _washing machine_ (WM) change,
// if new request (statusCode & url) is different from the current one,
// a redirection is performed,
// else continue to next middleware
laundromat
// add middleware functions, named _washing machines_ (WM)
// to distinguish them from common Connect middleware
.use(function whirlpool(req, res, next){
// some logic ... then ...
// change nothing and go to the next WM
return next();
// stop WM execution stack
// and pass an error to the next middleware
return next(new Error('Wow'));
// trigger a redirection
//
// `res.redirect` is actually a stubbed version of Express' method
// that is restored after the laundromat has finished iterating
//
// in this case redirect changes `req`'s `url`,
// then the laundromat WM stack is replayed
//
return res.redirect(303, 'http://so.me/st/uff');
})
.use(laundromat.whites) // laundromat-attached washing machine, just for fun
.use(laundromat.delicates)
.use(laundromat.wool)
;
Then use laundromat middleware in an Express app for example :
app.use(laundromat.wash);
Nope.
Number of loops is limited at an average of 2 per middleware. If max number is reached, you probably have a redirection loop problem and an error is passed to the next middleware.
--------
| MW A |
--------
|
v
----------------
| Laudromat MW |--------------|
| | v
-------- <-------
| | | MW B | |
INSERT -------- <--- |
| COIN | | | |
v | | (req, res) objects possibly looping
| || | -------- | | when redirection triggered
|| | MW C |----| | by MW B, C or C
| | -------- |
| |
| ____ | v |
| | -------- |
| |__| | | MW D |--------|
--------
| | |
| Laudromat MW |<-------------|
----------------
|
v
--------
| MW E |
--------