Created
August 16, 2010 23:33
-
-
Save shimondoodkin/527981 to your computer and use it in GitHub Desktop.
<shimondoodkin> i have a problem with http if i define the on end and on data callbacks after like 120 ms it does not call callbacks or maybe if it is clouse inside a clousre, the object looks ok but it does not fire the events [01:52] <bradleymeck> is t
This file contains 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'); | |
var url = require('url'); | |
//var sys = require('sys'); | |
var mylog="log:\r\n"; | |
/* | |
<shimondoodkin> i have a problem with http if i define the on end and on data callbacks after like 120 ms it does not call callbacks or maybe if it is clouse inside a clousre, the object looks ok but it does not fire the events | |
[01:52] <bradleymeck> is the request done by 120ms? | |
[01:52] <shimondoodkin> it hangs | |
[01:52] <shimondoodkin> after a minute it fails | |
*/ | |
function log(str) | |
{ | |
mylog+=str+"\r\n"; | |
//console.log(str); // console.log and sys.inspect are slow, they fails even the ok-case | |
} | |
http.createServer(function (request, response) | |
{ | |
switch (url.parse(request.url).pathname) | |
{ | |
case '/': | |
//show form | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.write( | |
'<form action="/ok_case" method="POST">' + | |
'choose file: <input type="file" name="upload-file"><br>' + | |
'enter some text: <input type="text" name="description"><br>' + | |
'<input type="submit" value="OK">' + | |
'</form>'+ | |
'<form action="/failing_case" method="POST">' + | |
'choose file: <input type="file" name="upload-file"><br>' + | |
'enter some text: <input type="text" name="description"><br>' + | |
'<input type="submit" value="Will Fail">' + | |
'</form> <br><br> <a href="/log">log</a>, <a href="/clearlog">clear log</a>' | |
); | |
response.end(); | |
break; | |
case '/log': | |
//show form | |
response.writeHead(200, {'Content-Type': 'text/plain'}); | |
response.write( mylog ); | |
response.end(); | |
break; | |
case '/clearlog': | |
mylog=""; | |
response.writeHead(200, {'Content-Type': 'text/plain'}); | |
response.write( "log cleared" ); | |
response.end(); | |
break; | |
case '/ok_case': | |
log("ok_case - request(just before): " /* +sys.inspect(request)*/ ); | |
setTimeout(function() | |
{ | |
log('method: '+request.method); | |
if(request.method=='POST') | |
{ | |
log("ok_case - method POST , just define events"); | |
var data=''; | |
request.on('data', function(chunk) { log("ok_case - on data : "+chunk.length); data += chunk; }); | |
request.on('end', function() | |
{ | |
log("ok_case - on end"); | |
response.writeHead(200, { 'Content-Type': 'text/html'}); | |
response.write("ok_case - posted data: "+data); | |
response.end(); | |
}); | |
log("ok_case - method POST , end define events" /*+", request: " +sys.inspect(request._events)*/ ); | |
} | |
else | |
{ | |
log("ok_case - method GET"); | |
response.writeHead(200, { 'Content-Type': 'text/html'}); | |
response.write('ok_case - method GET'); | |
response.end(); | |
} | |
},2); | |
break; | |
case '/failing_case': | |
log("failing_case - request(just before): " /*+sys.inspect(request) */); | |
setTimeout(function() | |
{ | |
log('method: '+request.method); | |
if(request.method=='POST') | |
{ | |
log("failing_case - method POST , just define events"); | |
var data=''; | |
request.on('data', function(chunk) { log("failing_case - on data : "+chunk.length); data += chunk; }); | |
request.on('end', function() | |
{ | |
log("failing_case - on end"); | |
response.writeHead(200, { 'Content-Type': 'text/html'}); | |
response.write("failing_case - posted data: "+data); | |
response.end(); | |
}); | |
log("failing_case - method POST , end define events" /* + ", request: " +sys.inspect(request._events)*/ ); | |
} | |
else | |
{ | |
log("failing_case - method GET"); | |
response.writeHead(200, { 'Content-Type': 'text/html'}); | |
response.write('failing_case - method GET'); | |
response.end(); | |
} | |
},50); | |
break; | |
default: | |
//show form | |
response.writeHead(200, {'Content-Type': 'text/plain'}); | |
response.write( "the url is: "+url.parse(request.url).pathname ); | |
response.end(); | |
break; | |
} | |
}).listen(8124); | |
console.log('Server running at http://127.0.0.1:8124/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment