Skip to content

Instantly share code, notes, and snippets.

@mubix
Created July 23, 2014 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mubix/3aadc8210bf3d80163b9 to your computer and use it in GitHub Desktop.
Save mubix/3aadc8210bf3d80163b9 to your computer and use it in GitHub Desktop.
Fun with NodeJS
var http = require('http');
function parseSwitch(req){
var list = {},
rc = req.headers.cookie;
rc && rc.split(';').forEach(function( cookie ) {
var parts = cookie.split('=');
list[parts.shift().trim()] = unescape(parts.join('='));
});
return list;
};
http.createServer(function (req, res) {
var sw = parseSwitch(req);
if(sw.Switch == 1){
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': 'Switch=2'
});
res.write('<script type="text/javascript">window.location = "' + req.url + 'b' + '"</script>');
}
else {
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': 'Switch=1'
});
res.write('<script type="text/javascript">window.location = "' + req.url + 'a' + '"</script>');
};
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
console.log(req.connection.remoteAddress + ' going to ' + req.url);
res.end('\n<h1>Isn\'t this fun?</h1>\n');
}).listen(80, '0.0.0.0');
console.log('Server has started...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment