forked from joehewitt/express-rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewrite.js
26 lines (24 loc) · 840 Bytes
/
rewrite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function rewriter(req, res, next) {
req.app.routes.get.forEach(function (item, index) {
if (item.match(req.url)){
item.callbacks.forEach(function(callback) {
if (callback && callback.rewriteTarget) {
req.urlRewritten = req.url;
req.url = req.url.replace(item.regexp, callback.rewriteTarget);
}
});
}
});
next();
}
rewriter.rewrite = function(target) {
var handler = function(req, res, next) {
// This route should never ever be handler because it will be
// intercepted by the rewriter middleware before it gets here. If this
// ever gets called, it means you forgot to use the rewriter middleware.
res.send(500);
};
handler.rewriteTarget = target;
return handler;
}
module.exports = rewriter;