Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I'm using Node.js and trying to render an EJS 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 an EJS template file?

like image 481
Headshota Avatar asked Dec 28 '11 20:12

Headshota


People also ask

How do I render an EJS file in nodeJS?

Use ejs. renderFile(filename, data) function with async-await. To render HTML files. To render EJS files.

What is EJS file in node JS?

EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.


1 Answers

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

like image 193
ksloan Avatar answered Oct 23 '22 03:10

ksloan