Last active
December 21, 2015 01:49
-
-
Save dazld/6230730 to your computer and use it in GitHub Desktop.
Demonstrating race conditions using globals in an express app.
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 express = require('express'), | |
app = express(), | |
userLoggedIn = false; // global shared between requests (just to show it not working, could be cookie based) | |
app.all('*', function(req, res, next) { | |
var id = Date.now(); | |
userLoggedIn = Math.random() > 0.5 ? true : false; // imagine that this is based on some API call | |
console.log('Rx request id %s with userLoggedIn as %s', id, userLoggedIn); | |
var timer = Math.random() * 2500; // random response time | |
setTimeout(function() { | |
console.log('sending response id %s with userLoggedIn as %s', id, userLoggedIn); | |
var someResponse = userLoggedIn ? 'Hello User' : 'Not Logged In'; | |
res.send(someResponse); | |
}, timer); | |
}); | |
app.listen(8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment