3

I want to send the <head> containing stylesheets before processing the rest of the page. In PHP, I could use ob_flush().

I tried doing something like this:

app.set('view engine','ejs');

app.get('*',function(req,res){
  res.writeHead(200,{'Content-Type':'text/html'});
  res.write('<!doctype...<link rel=stylesheet...');

  doDBStuff()
    .then(function(data){
      res.render('index',{...}); // uses EJS for templating
    });
});

However, the res.render() part does not get sent. Is there a built-in way to send chunked data?

One way to do it would be to manually load the EJS files and manually process them. I would also have to manually send the appropriate headers. I prefer a build-in method if it exists.

8
  • 1
    You should put your res.render() inside the callback of your db functions. As it is there, your render won't have anything to render. Can you add in what the shell of your db call looks like? Commented Jul 23, 2015 at 3:00
  • @remus: It's there for simplicity, I guess I could change it.. Commented Jul 23, 2015 at 3:03
  • Ahhh ok. Makes a world of difference. So that still doesn't work then? Commented Jul 23, 2015 at 3:06
  • An alternative could be to send a page with all desired header info as a holder, then onDOMReady in that triggers an ajax call to get the DB stuff above Commented Jul 23, 2015 at 3:26
  • @CarlK A lot of the stuff is already loaded using Ajax. The main issue isn't the DB stuff, it's the stylesheets. The browser waits until all the HTML is downloaded before it starts downloading the stylesheets. I want it to start downloading the stylesheets ASAP. Commented Jul 23, 2015 at 3:33

1 Answer 1

5

Here's a simple PoC that does what you want:

var express = require('express');
var app     = express();
var server  = app.listen(3000);

app.set('views', 'views');
app.set('view engine', 'ejs');

app.get('/', function (req, res, next) {
  res.write('<!-- prefix -->');
  setTimeout(function() {
    res.render('index', { ... }, function(err, html) {
      // TODO: handle errors
      res.end(html);
    });
  }, 1000);
});

Note that it uses the "callback variant" of res.render(), which can be used to just render a template without sending back a response (if you don't do it this way, res.render() will generate an error). Alternatively, you can use app.render() which does the same.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.