How do I render an EJS template file in Node.js?

There is a function in EJS to render files, you can just do:

    ejs.renderFile(__dirname + '/template.ejs', function(err, data) {
        console.log(err || data);
    });

Source: Official EJS documentation


var templateString = null;
var fs = require('fs');
var templateString = fs.readFileSync('template.ejs', 'utf-8');

and then you do your thing:

var server = http.createServer(function(req, res){
    res.end(ejs.render(templateString));
});

Tags:

Node.Js

Ejs