我正在尝试从文件中呈现 EJS 模板,但出现错误 this.templateText.replace is not a function
const http = require('http');
const fs = require('fs');
const ejs = require('ejs');
const server = http.createServer(function(req, res){
fs.readFile('index.ejs', function(err, data) {
if (err) {
res.end("Error");
}
res.end(ejs.render(data, { title: "Hello" }));
});
});
server.listen(4000);
最佳答案
事实证明,fs.readFile
在回调 data
中返回一个原始缓冲区,而 ejs.redner
需要一个字符串。
If no encoding is specified, then the raw buffer is returned.
如果你想从 fs.readFile
中获取一个字符串,那么你需要将编码作为第二个参数传递:
fs.readFile('index.ejs', 'utf-8', function(err, data) {
// now data is a string
});
关于javascript - 渲染 EJS 模板报错 this.templateText.replace is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45123428/