I have a situation where I need multiple renders to occur with the same response object in an ExpressJS application. (Basically one HTTP request triggers multiple back-end requests, all of which can begin rendering results to the page immediately as they complete.) The problem is that I need each one to render a view (i.e. I don’t think I can use res.write()), and as far as I can tell, there is no way for res.render() to not end the response or write headers each time it is called.
What am I missing?
Express compiles the template using an engine like EJS, Jade etc.
The data is then rendered using response.send: https://github.com/visionmedia/express/blob/master/lib/response.js#L76-131
As you can see there, at the end there is
this.end..., which means response.end(…).If you want do achieve sending multiple views, you must compile those views yourself using the view engine and then create a function similar to response.send (which I gave you the link above), but be careful not to send the headers twice or call response.end before the last view is rendered.