Skip to content

Instantly share code, notes, and snippets.

@thibauts
Last active August 3, 2018 11:52
Show Gist options
  • Save thibauts/8834305 to your computer and use it in GitHub Desktop.
Save thibauts/8834305 to your computer and use it in GitHub Desktop.
express rerouting middleware
//
// middleware module
//
module.exports.reroute = function(url, func) {
return function(req, res, next) {
if(req.url.split('?', 1).pop() === url) {
req.url = func(req, res);
req.authorized = true;
}
next();
}
};
//
// usage
//
app.use(middleware.reroute('/v1/user', function(req, res) {
return '/v1/users/' + req.user.id;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment