I'm trying to do what should be the simplest process of passing a variable to an ejs template to display in the browser.
I read a file that contains only one string and assign the output to a variable. This works fine and i can view the contents with console.log. However, i can't figure out how to code the app.get and res.render part to send that variable to ejs (index.ejs).
var fs = require('fs');
// Read the contents of the file into memory.
fs.readFile('testfile.txt', function (err, log) {
// If an error occurred, throwing it will
// display the exception and end our app.
if (err) throw err;
// log is a Buffer, convert to string.
var text = log.toString();
console.log('The content of the log is' + text);
});
// index page
app.get('/', function(req, res) {
res.render('index', { var: text }
});
app.listen(8080);
console.log('8080 port');
Thanks.
<%= text %>
15| 16| 17| text is not defined How do i render my "text" variable and make it show up via ejs? – Guy Lacroix Dec 02 '15 at 01:16