sznrbrt
sznrbrt

Reputation: 1013

Mustache does not render values, when deployed

Node version: v8.9.0;

Im using moustache to render some html from templates as follows:

static createHtml(title, variables, template) {
  let header = fs.readFileSync(headerPath);
  let content = fs.readFileSync(contentPath);
  let footer = fs.readFileSync(footerPath);

  var headerVars = {
    title: title
  };

  console.log('createHtml: ', variables); // <- variables are as expected

  try {
    header = Mustache.render(header.toString(), headerVars);
    content = Mustache.render(content.toString(), variables);
  } catch (e) {
    console.log('Moustache error:\n', e); // <- no error is thrown
  }

  const html = header + content + footer;

  console.log('content', content); // <- content does not include filled variables when deployed

  return html;
}

When I run it on my local machine everything works just perfectly, however when it is deployed the variables are not injected into the template. I had several assumptions and tried to figure out every possible way the environment is different then the local, but nothing worked so far. Its is deployed on EC2 to an AMI Linux instance.

Any idea, how could I figure out what's the difference? According to the console logs it must be something inside Mustache.render().

Upvotes: 0

Views: 598

Answers (1)

sznrbrt
sznrbrt

Reputation: 1013

Seems that the deployed instance on AMI Linux did not really like the syncronous fs.readFileSync operation after one point (what was the point and why is still and open question), though the solution was very simple. Just refactor the code to async as follows:

static createHtml(title, variables, template) {
  fs.readFile(headerPath, (err, header) => {
    fs.readFile(contentPath, (err, content) => {
      fs.readFile(footerPath, (err, footer) => {

        var headerVars = { title: title };

        header = Mustache.render(header.toString(), headerVars);
        content = Mustache.render(content.toString(), variables);

        const html = header + content + footer.toString();

        return html;
      });
    });
  });
}

Upvotes: 1

Related Questions