ejs 错误 'include' 需要 'filename' 选项

ejs error 'include' requires the 'filename' option

问题

通过一些在线示例,您可以使用这样的语法

<%- include hello-world %>

甚至你可以使用

<%- include('hello-world'); %>

您可能会收到包含缺少文件名的错误

Exception occurred: Error: `include` requires the 'filename' option.

问如果我的语法正确,问题出在哪里?

答案在后端“文件路径

但即使您使用了正确的路径,您也会遇到错误

var fs = require('fs');
ejs.render(fs.readFileSync(__dirname + '/templates/include.ejs', 'utf8'), {});

正确答案是“使用 renderFile

ejs.renderFile(__dirname + '/templates/include.ejs', {}, function(err, result) {
    if (!err) {
        res.end(result);
    }
    else {
        res.end(err.toString());
        console.log(err);
    }
});

参考