是否可以将 node.js 视图输出缓存为 html 文件?
Is it possible cache node.js view output as a html file?
在PHP中,我曾经使用output buffering to cache the output,保存为html文件。我想知道是否可以在 node.js 中完成同样的操作。以下是我的路由文件:
module.exports = {
index: function(section,page,req,res){
var l = res.locals,
db = l.db,
Promise = l.Promise,
Promise.props({
my: db.db.query('CALL fetchdata()'),
amba: db.db.query("CALL fetchanother()")
}).then(function(obj){
return res.render(section+'.html',obj)
}).then(function(data){
console.log(data);
l.fs.writeFile('index.html', data)
}).catch(function (error) {
console.log(error);
})
}
};
return res.render(section+'.html',obj)
无效。 console.log(data)
returns "undefined" 在控制台和 html 文件中除了单词 "undefined" 之外什么都没有。我也试过这个:
.then(function(obj){
var cache
res.render(section+'.html',obj,function(k,content){
res.send(content)
cache = content
})
return cache;
}).then(function(data){
console.log(data);
l.fs.writeFile('index.html', data)
})
仍未定义。有没有办法将视图结果缓存为 html 文件?
在第一个片段中,data
是 undefined
因为这是 res.render(...)
returns.
的值
通常(取决于具体的 Promise 实现),.then()
回调中返回的 Promise
以外的任何值都将被视为解析值。因此,以下 2 个片段大致相同。
.then(function () {
return undefined;
})
.then(function () {
return new Promise(function (resolve) {
resolve(undefined);
});
})
要接收 html
,因为 res.render()
是异步的并且本身不提供 Promise,您需要将其包装在 Promise 中以便等待:
.then(function(obj){
return new Promise(function (resolve, reject) {
res.render(section+'.html', obj, function (err, html) {
if (err)
reject(err);
else
resolve(html);
});
});
}).then(function(data){
// ...
注意:以上片段与 ES6 Promises 兼容,如果您使用不同的实现,可能需要修改。
对于第二个片段,已经有一个关于 SO 的问答并有很好的解释:
Why is my variable unaltered after I modify it inside of a function?
在PHP中,我曾经使用output buffering to cache the output,保存为html文件。我想知道是否可以在 node.js 中完成同样的操作。以下是我的路由文件:
module.exports = {
index: function(section,page,req,res){
var l = res.locals,
db = l.db,
Promise = l.Promise,
Promise.props({
my: db.db.query('CALL fetchdata()'),
amba: db.db.query("CALL fetchanother()")
}).then(function(obj){
return res.render(section+'.html',obj)
}).then(function(data){
console.log(data);
l.fs.writeFile('index.html', data)
}).catch(function (error) {
console.log(error);
})
}
};
return res.render(section+'.html',obj)
无效。 console.log(data)
returns "undefined" 在控制台和 html 文件中除了单词 "undefined" 之外什么都没有。我也试过这个:
.then(function(obj){
var cache
res.render(section+'.html',obj,function(k,content){
res.send(content)
cache = content
})
return cache;
}).then(function(data){
console.log(data);
l.fs.writeFile('index.html', data)
})
仍未定义。有没有办法将视图结果缓存为 html 文件?
在第一个片段中,data
是 undefined
因为这是 res.render(...)
returns.
通常(取决于具体的 Promise 实现),.then()
回调中返回的 Promise
以外的任何值都将被视为解析值。因此,以下 2 个片段大致相同。
.then(function () {
return undefined;
})
.then(function () {
return new Promise(function (resolve) {
resolve(undefined);
});
})
要接收 html
,因为 res.render()
是异步的并且本身不提供 Promise,您需要将其包装在 Promise 中以便等待:
.then(function(obj){
return new Promise(function (resolve, reject) {
res.render(section+'.html', obj, function (err, html) {
if (err)
reject(err);
else
resolve(html);
});
});
}).then(function(data){
// ...
注意:以上片段与 ES6 Promises 兼容,如果您使用不同的实现,可能需要修改。
对于第二个片段,已经有一个关于 SO 的问答并有很好的解释:
Why is my variable unaltered after I modify it inside of a function?