渲染一个variables到ejs模板
我正在同步读取一个文件并将内容发送给一个variables:
// Load the fs (filesystem) module var fs = require('fs'); // Read the contents of the file into memory. var text = fs.readFileSync('testfile.txt').toString() // index page app.get('/', function(req, res) { res.render('index', { var: text }); }); app.listen(8080); console.log('8080 port');
然后在我的ejs文件(index.ejs)中,我插入了像这样的variables:
<main> <div class="jumbotron"> <h2><%= text %></h2> </div> </main>
我得到这个错误,没有引用“文本”:
>> 14| <h2><%= text %></h2> 15| </div> 16| </main> 17| </footer> text is not defined
你如何呈现一个variablesejs?
// Load the fs (filesystem) module var fs = require('fs'); // Read the contents of the file into memory. var text = fs.readFileSync('testfile.txt').toString() // index page app.get('/', function(req, res) { res.render('index', { text: text }); }); app.listen(8080); console.log('8080 port');
当您将页面呈现为文本时,请不要从variables声明中删除var关键字,而应删除var关键字。
它应该工作。