public
Last active

Node.js HTTP streaming example (Event connection close doesn't work on heroku).

  • Download Gist
Procfile
1
web: node web.js
README.md
Markdown
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13
{
"name": "node-example",
"version": "0.0.1",
"dependencies": {
"express": "3.1.x",
"mongodb" : "1.1.11",
"redis-url": "0.1.0"
},
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
}
}
web.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
var express = require("express");
var app = express();
app.use(express.logger());
 
app.get('/', function(req, res) {
 
res.writeHead(200, {'Content-type': 'text/javascript'});
 
req.connection.on('close',function(){
// code to handle connection abort
console.log("closed connection");
clearInterval(interval);
res.end();
});
 
var interval = setInterval(function(){
res.write('Body data.\n');
console.log("Still processing a response.");
}, 1000);
});
 
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.