Make Better Things



I like to make better things.

How to use HTML with Express (Node.js)

There is no direct way of using HTML under Jade, which comes with Express by default, But if you want to use more HTML-style templates, you could use els instead of Jade. Follow these steps to use ejs instead of Jade -

1) Install ejs –

npm install ejs

2) Set your template engine in app.js as ejs


// app.js
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

3) Now in your route file you can assign template variables –

// ./routes/index.js
exports.index = function(req, res){
  res.render('index', { title: 'ejs' });
};

4) Then you can create your html view in /views directory.

// ./views/index.html
<h1>Welcome to <%= title %>!</h1>

Category: node.js

Tagged: , ,

5 Responses

  1. eric says:

    Wouldn’t you be running a totally unnecessary process on every page you serve now? If you want to use EJS this is cool, but it seems unnecessary if you are trying to serve plain html.

  2. Don says:

    This was exactly what I needed to quickly install and test ejs. Thanks

  3. vardha says:

    if i were to use the title variable in a unload javascript function in the same ejs page, how would i use it . for instance

    window.onload(){
    var s =
    alert(s);
    }

    this function produces a console error saying
    “caught syntax error: Unexpected identifier”

  4. ifeelmyself says:

    Thanks a lot!

Leave a Reply