I'm trying to render an EJS template from file but I'm getting an error 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);
It turns out that fs.readFile
returns a raw buffer in callback data
while ejs.redner
is expecting a string.
If no encoding is specified, then the raw buffer is returned.
If you want to get a string from fs.readFile
then you need to pass encoding as a second argument:
fs.readFile('index.ejs', 'utf-8', function(err, data) {
// now data is a string
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With