Besides this post javascript (nodejs) while loop bug which is a different issue with the "while" loop in node js, I can't seem to understand why the following code doesn't 'block.' I am trying to get the event loop to block so that I can block requests (this is just for testing/pleasure and getting familiar with nodejs).
http = require('http')
data = ""
processData = (chunk) ->
date = new Date()
dm = date.getMilliseconds()
curDate = null
loop
curDate = (new Date()).getMilliseconds()
break if curDate-dm > 4000
data += chunk
endData = ->
console.log(data)
data = ""
requestListener = (req,res) ->
req.on "data", processData
req.on "end", endData
res.writeHead 200
res.write "hello buddy"
res.end()
webserver = http.createServer requestListener
webserver.listen(6666)
I am expecting some sort of blocking for 4 seconds, and then console displaying some data from the request via
curl -d "testdata=32" "http://localhost:6666"
however, node never writes anything to the console nor sends a response back to client and just 'freezes.' If i increase the time to 100 milliseconds it works for the first 5-8 tries (sends data back to curl) but eventually freezes. Am I missing something here? is this a bug? Shouldn't node handle this gracefully?
I have encountered other methods for blocking that do work such as:
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + 4000);
My env: windows 7/cywin 2.774/nodejs 0.8.10/ curl 7.27.0