Skip to content

Instantly share code, notes, and snippets.

@Sequoia
Created January 19, 2016 22:19
Show Gist options
  • Save Sequoia/8f209265b9eb4b372afa to your computer and use it in GitHub Desktop.
Save Sequoia/8f209265b9eb4b372afa to your computer and use it in GitHub Desktop.
Lucky Middleware
var app = require('express')();
var luck = require('./luck-filter');
var prettyLucky = luck(0.6);
var reallyQuiteLucky = luck(0.9);
app.use('/blog', prettyLucky);
//bad luck is contagious; only allow luckiest of all users to contact you
app.use('/contact', prettyLucky);
/**
* @param float 0-1 minimum luck threshold (higher = more lucky)
* default: .5
*/
module.exports = function(threshold){
threshold = threshold || 0.5;
return function luckyUsersOnly(req, res, next){
if(Math.random() < threshold){
res.status(403).send('Not Authorized (Unlucky User)');
}
else{
next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment