部署时,Mustache不呈现值

时间:2018-02-16 00:44:52

标签: node.js templates mustache

节点版本:v8.9.0;

我使用小胡子从模板中渲染一些html,如下所示:

<root>
  <array>
    <LearningActivityKey>2122</LearningActivityKey>
    <ModuleName>certName</ModuleName>
    <!-- Additional properties omitted -->
  </array>
  <array>
    <LearningActivityKey>2122</LearningActivityKey>
    <ModuleName>certName</ModuleName>
    <!-- Additional properties omitted -->
  </array>
</root>

当我在我的本地机器上运行它时,一切都很完美,但是当它部署时,变量不会注入到模板中。我有几个假设,并试图找出环境与当地不同的所有可能方式,但到目前为止没有任何工作。它在EC2上部署到AMI Linux实例。

任何想法,我怎么能弄清楚它们的区别?根据控制台日志,它必须是Mustache.render()中的内容。

1 个答案:

答案 0 :(得分:1)

似乎AMI Linux上部署的实例在一点之后并不真正喜欢同步的fs.readFileSync操作(虽然解决方案非常简单,但是为什么仍然是开放式问题)。只需将代码重构为async,如下所示:

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;
      });
    });
  });
}