4

Here is my code:

var express = require('express'),
      app = express.createServer(express.logger());
app.use(express.bodyParser());
app.get('/', function(request, response) {
    var name = request.param('name', null);
    response.header('Content-Type', 'text/event-stream');
    var t = setInterval(function(){
        response.write('Hello World by '+name+' (using http-1.1 streaming data!)<br />');
    }, 2000);
    /*request.socket.on('close', function() {
        clearInterval(t);
    }); */
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
    //clearInterval(t);

  console.log("Listening on " + port);
});

I am using express because I am testing this in heroku and I used the guide there. So when I open the app I get a download popup with the data in the file (downloaded) instead of the html (dom) response.

What did I do wrong? (is it a browser problem?)

Edit:
@Joe, has solved my initial problem but yet, I have two questions:
1) Why i don't need to insert a 'correct' content type? (of streaming?)
2) Why does this code out put only after along time (about half a minute).?

funerr
  • 7,212
  • 14
  • 81
  • 129
  • 1
    This might help explain what `text/event-stream` does http://dsheiko.com/weblog/html5-and-server-sent-events/ – Joe Jul 31 '12 at 00:31

1 Answers1

4

If you are trying to display it as HTML you will want your Content-Type to be text/html

Joe
  • 2,987
  • 15
  • 24
  • But how will I stream the data? in every interval lap? – funerr Jul 30 '12 at 21:33
  • & It works, but it loads for a long time (about 22 seconds) until it even shows something, why? – funerr Jul 30 '12 at 21:40
  • Does it take 22 seconds the first time, then each time after seem very quick? It could be using some development setup on boot. I don't know NodeJS well enough. – Joe Jul 31 '12 at 00:32
  • Hi @funerr, I also had to set response.header('Accept', 'text/html'), in addition to response.header('Content-Type', 'text/html') – mdiehl13 Jan 10 '20 at 01:32