Created
July 23, 2014 18:28
-
-
Save mubix/3aadc8210bf3d80163b9 to your computer and use it in GitHub Desktop.
Fun with NodeJS
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 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