Created
January 19, 2016 22:19
-
-
Save Sequoia/8f209265b9eb4b372afa to your computer and use it in GitHub Desktop.
Lucky Middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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