我正在从最简单的开始学习node和express,当使用res.render('view',{data:data})渲染 View 时,是否只有像jade这样的模板引擎适合 View 。我可以不使用普通的 html 吗?
最佳答案
可以,但这是我学习Node时遇到的问题。如果您不想使用模板引擎,您仍然可以让 Node 以静态方式吐出 HTML 文件的内容。例如(非常基本的示例):
var base = '/path/to/your/public_html',
fs = require('fs'),
http = require('http'),
sys = requrie('sys');
http.createServer(function (req,res) {
path = base + req.url;
console.log(path);
path.exists(path, function(exists) {
if(!exists) {
res.writeHead(404);
res.write('Bad request: 404\n');
res.end();
} else {
res.setHeader('Content-Type','text/html');
res.statusCode = 200;
var file = fs.createReadStream(path);
file.on("open",function() {
file.pipe(res);
});
file.on("error",function(err) {
console.log(err);
});
}
});
}).listen(80);
console.log('server on tcp/80');
关于node.js - 我是否只需使用具有快速渲染功能的模板语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061860/