Headshota Headshota - 1 year ago 115
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'));


How can I render a template file?

Answer Source
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){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download