小胡子在部署时不呈现值
Mustache does not render values, when deployed
节点版本:v8.9.0;
我使用小胡子从模板中渲染一些 html,如下所示:
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;
}
当我 运行 它在我的本地机器上时一切正常,但是当它被部署时变量没有注入到模板中。我有几个假设,并试图找出环境与当地环境不同的每一种可能方式,但到目前为止没有任何效果。它在 EC2 上部署到 AMI Linux 实例。
任何想法,我怎么能找出有什么区别?根据控制台日志,它一定是 Mustache.render().
里面的东西
似乎在 AMI Linux 上部署的实例在一点之后并不真正喜欢同步 fs.readFileSync 操作(重点是什么,为什么仍然是悬而未决的问题),尽管解决方案是很简单的。只需将代码重构为异步,如下所示:
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;
});
});
});
}
节点版本:v8.9.0;
我使用小胡子从模板中渲染一些 html,如下所示:
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;
}
当我 运行 它在我的本地机器上时一切正常,但是当它被部署时变量没有注入到模板中。我有几个假设,并试图找出环境与当地环境不同的每一种可能方式,但到目前为止没有任何效果。它在 EC2 上部署到 AMI Linux 实例。
任何想法,我怎么能找出有什么区别?根据控制台日志,它一定是 Mustache.render().
里面的东西似乎在 AMI Linux 上部署的实例在一点之后并不真正喜欢同步 fs.readFileSync 操作(重点是什么,为什么仍然是悬而未决的问题),尽管解决方案是很简单的。只需将代码重构为异步,如下所示:
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;
});
});
});
}