Skip to content

Instantly share code, notes, and snippets.

@narqo
Last active December 21, 2015 22:29
Show Gist options
  • Save narqo/6375881 to your computer and use it in GitHub Desktop.
Save narqo/6375881 to your computer and use it in GitHub Desktop.
Test for http request and the globals
var http = require('http'),
util = require('util'),
port = process.env.PORT || 3014,
counter = 1;
/* global lang:true */
lang = 'ru';
function onRequest(req, res) {
counter += 1; // Oops
// user's language is depends on some freaky logic...
lang = counter % 2 === 0? 'ru' : 'en'; // Oops
console.log(counter);
console.log(lang);
setTimeout(function() {
var tmpl = util.format('We got %d request and the language is set to <b>%s</b>\n',
counter, lang);
res.writeHead(200, {
'Content-Type' : 'text/html'
});
res.end(tmpl);
}, 3000);
}
http.createServer(onRequest).listen(port);
› ((curl "http://localhost:3014" &) ; curl "http://localhost:3014" &) 2> /dev/null
We got 3 request and the language is set to <b>en</b> # ← this one should be 2nd with "ru"
We got 3 request and the language is set to <b>en</b>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment