Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML in Express instead of Jade

How to I get rid of Jade while using Express with Node.JS? I want to just use plain html. In other articles I have seen that people recommended app.register() which is now deprecated in the latest version.

like image 838
Sanchit Gupta Avatar asked Jul 15 '12 21:07

Sanchit Gupta


People also ask

Can I use HTML in Express?

You don't need to install any extra modules to render an HTML file in Express. Just install express and you are good to go.

Do you have to use jade with Express?

You can continue to use Jade in your app, and it will work just fine. However if you want the latest updates to the template engine, you must replace Jade with Pug in your app. To render template files, set the following application setting properties, set in app.

Why do we use EJS instead of HTML?

EJS simply stands for Embedded Javascript. It is a simple templating language/engine that lets its user generate HTML with plain javascript. EJS is mostly useful whenever you have to output HTML with a lot of javascript.

Can we set view engine as HTML?

set('view engine', 'html'); add your view templates as . html inside “views” folder. Restart you node server and start the app in the browser.


1 Answers

You can do it this way:

  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.

like image 151
Biwek Avatar answered Sep 28 '22 03:09

Biwek