1

I'm aiming to move from Jade back to raw HTML in my Express app, but I want to make sure I'm not losing any advantages of a templating engine, so I want a solution that:

  • Will cache the HTML
  • Allows me to specify a path relative to the views directory (like Jade does) without adding path.join(__dirname, "views") to every route.

What's the best solution here?

Migwell
  • 18,631
  • 21
  • 91
  • 160

1 Answers1

1

To solve that problem I'm using the simple sendFile method from express response object. Here's a use case:

var express = require('express');
var router = express.Router();
var path = require('path');

var views = function (view) { 
    path.join(__dirname, '../views/', view);
};

router.get('/', function (req, res) {
    res.sendfile(views('index.html'));
});

Note that you can write the views function in another file, export and require it in every router that you want.

The sendFile method accepts a maxAge parameter that you can use for caching purposes.

And you're right about the template engine overhead. In my tests, I got 120~150ms faster response times from server, using raw html instead of jade with html imports.

William Dias
  • 111
  • 5