Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created December 13, 2011 02:25
Show Gist options
  • Save gjohnson/1470178 to your computer and use it in GitHub Desktop.
Save gjohnson/1470178 to your computer and use it in GitHub Desktop.
Delayed Response
var http = require('http');
http.createServer(function(req, res) {
var url = req.url;
switch (url) {
case '/engagement.js':
var timer = setTimeout(function() {
clearTimeout(timer);
console.log('client is still here after two seconds.');
var data = 'window.__data = { success: true };';
res.writeHead(200, {
'Content-Length': data.length,
'Content-Type': 'application/x-javascript',
'Connection': 'close'
});
res.end(data);
}, 2000);
req.connection.on('end', function() {
clearTimeout(timer);
console.log('client closed connection before 2 seconds.');
});
break;
default:
var data = '<html><head><script src="/engagement.js"></script></head><body><h1>Hello</h1></body></html>';
res.writeHead(200, {
'Content-Length': data.length,
'Content-Type': 'text/html'
});
res.end(data);
}
}).listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment