How do I use HTML as the view engine in Express? How do I use HTML as the view engine in Express? express express

How do I use HTML as the view engine in Express?


The answers at the other link will work, but to serve out HTML, there is no need to use a view engine at all, unless you want to set up funky routing. Instead, just use the static middleware:

app.use(express.static(__dirname + '/public'));


To make the render engine accept html instead of jade you can follow the following steps;

  1. Install consolidate and swig to your directory.

     npm install consolidate npm install swig
  2. add following lines to your app.js file

    var cons = require('consolidate');// view engine setupapp.engine('html', cons.swig)app.set('views', path.join(__dirname, 'views'));app.set('view engine', 'html');
  3. add your view templates as .html inside “views” folder. Restart you node server and start the app in the browser.

Though this will render html without any issue, I would recommend you to use JADE by learning it. Jade is an amazing template engine and learning this will help you achieve better design & scalability.


In your apps.js just add

// view engine setupapp.set('views', path.join(__dirname, 'views'));app.engine('html', require('ejs').renderFile);app.set('view engine', 'html');

Now you can use ejs view engine while keeping your view files as .html

source: http://www.makebetterthings.com/node-js/how-to-use-html-with-express-node-js/

You need to install this two packages:

npm install ejs --savenpm install path --save

And then import needed packages:

var path = require('path');


This way you can save your views as .html instead of .ejs.
Pretty helpful while working with IDEs that support html but dont recognize ejs.