Headshota Headshota - 4 months ago 27
Node.js Question

Render ejs file in node.js

Hey guys I'm playing with node.js and trying to render an template file. I figured out how to render strings:

var http = require('http');
var ejs = require('ejs');

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

server.listen(3000);


How can I render a template file?

Answer
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));
});