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>

5 thoughts on “How to use HTML with Express (Node.js)”

  1. 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. 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

    var s =

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

Leave a Reply

Your email address will not be published. Required fields are marked *