erik erik - 1 year ago 169
Node.js Question

Express Render Template from String

When a GET request is made on my site, I am trying to render templates that are stored on another server. Essentially my application will make a request to this server, and the response will contain the template that I would like to use. The server's response is a dust template in the form of a string.

What is the best way to render the template?

router.get('/', function(req, res) {
var options = {
host: myHost,
path: myPath,
port: myPort};

var templateReq = http.get(options, function(response) {
var templateStr = '';

response.on('data', function(chunk) {
templateStr += chunk;

response.on('end', function() {
var dustParams = myDustParams;
res.render(templateStr, dustParams); //THIS DOES NOT WORK

I understand that render looks in the 'views' folder to search for a template.

Also, I know that you can directly send HTML to the response using 'send', but by doing this I am unable to include the dust parameters.

Answer Source

You can use:


Regarding having dust populate the params in the template, you have to compile and render it before using the res.send

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download