Skip to content

Instantly share code, notes, and snippets.

@krasimir
Created April 17, 2014 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krasimir/10985676 to your computer and use it in GitHub Desktop.
Save krasimir/10985676 to your computer and use it in GitHub Desktop.
Demonstrating Node.js single threadness
var http = require('http');
var getTime = function() {
var d = new Date();
return d.getHours() + ':' + d.getMinutes() + ':' +
d.getSeconds() + ':' + d.getMilliseconds();
}
var respond = function(res, str) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(str + '\n');
console.log(str + ' ' + getTime());
}
var handleRequest = function (req, res) {
console.log('new request: ' + req.url + ' - ' + getTime());
if(req.url == '/immediately') {
respond(res, 'A');
} else {
var now = new Date().getTime();
while(new Date().getTime() < now + 5000) {
// synchronous reading of the file
}
respond(res, 'B');
}
}
http.createServer(handleRequest).listen(9000, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment