title: How to use multiple engines (ejs and hbs) with Node.js and Express
body: <p>I am trying to link (href) my <code>hbs</code> page to a <code>ejs</code> page in my <code>views</code> folder. I have installed consolidate as seen in other tutorials via going to the directory <code>npm install consolidate</code> and have added the code <code>var engines = require('consolidate');</code> and <code>app.engine('handlebars', engines.handlebars);</code> to my code but nothing works (I also know if you’re using “.ejs” extensions you don’t need to do anything). Can someone please help me out as I cannot view my pages due to this. I receive this error - "Failed to lookup view <code>employee/addOrEdit</code> in <code>views</code> directory...". I have added my folder/file structure below and code snippets. Thank you!
  <a href="https://i.stack.imgur.com/tNwul.png" rel="nofollow noreferrer">folder/file structure</a></p>
  
  <p>server.js file</p>
  
  <pre><code>require('./models/db');
  
  const express = require('express');
  const path = require('path');
  const exphbs = require('express-handlebars');
  const bodyparser = require('body-parser');
  
  var engines = require('consolidate');
  
  const employeeController = require('./controllers/employeeController');
  
  var app = express();
  
  app.engine('handlebars', engines.handlebars);
  
  app.use(bodyparser.urlencoded({
      extended: true
  }));
  app.use(bodyparser.json());
  app.set('views', path.join(__dirname, '/views/'));
  app.engine('hbs', exphbs({ extname: 'hbs', defaultLayout: 'mainLayout', layoutsDir: __dirname + '/views/layouts/' }));
  app.set('view engine', 'hbs');
  //render css files
  app.use(express.static("public"));
  
  app.listen(3000, () =&gt; {
      console.log('Express server started at port : 3000');
  });
  
  app.use('/employee', employeeController);
  </code></pre>
  
  <p>employeeController.js file</p>
  
  <pre><code>const express = require('express');
  var router = express.Router();
  const mongoose = require('mongoose');
  const Employee = mongoose.model('Employee');
  //placeholders for added task
  var task = ["complete presentation", "practise with nodejs"];
  //placeholders for removed task
  var complete = ["finish jquery"];
  
  
  router.get('/', (req, res) =&gt; {
      res.render("employee/addOrEdit", {
          viewTitle: "Insert Module"
      });
  });
  
  router.get('/test', (req, res) =&gt; {
      res.render("employee/test");
  });
  
  //render the ejs and display added task, completed task
  router.get("/index", function(req, res) {
      res.render("index", { task: task, complete: complete });
  });
  
  router.post('/', (req, res) =&gt; {
      if (req.body._id == '')
          insertRecord(req, res);
          else
          updateRecord(req, res);
  });
  
  
  function insertRecord(req, res) {
      var employee = new Employee();
      employee.fullName = req.body.fullName;
      employee.module = req.body.module;
      employee.mobile = req.body.mobile;
      employee.city = req.body.city;
      employee.save((err, doc) =&gt; {
          if (!err)
              res.redirect('employee/list');
          else {
              if (err.name == 'ValidationError') {
                  handleValidationError(err, req.body);
                  res.render("employee/addOrEdit", {
                      viewTitle: "Insert Module",
                      employee: req.body
                  });
              }
              else
                  console.log('Error during record insertion : ' + err);
          }
      });
  }
  
  function updateRecord(req, res) {
      Employee.findOneAndUpdate({ _id: req.body._id }, req.body, { new: true }, (err, doc) =&gt; {
          if (!err) { res.redirect('employee/list'); }
          else {
              if (err.name == 'ValidationError') {
                  handleValidationError(err, req.body);
                  res.render("employee/addOrEdit", {
                      viewTitle: 'Update Module',
                      employee: req.body
                  });
              }
              else
                  console.log('Error during record update : ' + err);
          }
      });
  }
  
  
  router.get('/list', (req, res) =&gt; {
      Employee.find((err, docs) =&gt; {
          if (!err) {
              res.render("employee/list", {
                  list: docs
              });
          }
          else {
              console.log('Error in retrieving module list :' + err);
          }
      });
  });
  
  
  function handleValidationError(err, body) {
      for (field in err.errors) {
          switch (err.errors[field].path) {
              case 'fullName':
                  body['fullNameError'] = err.errors[field].message;
                  break;
              case 'module':
                  body['moduleError'] = err.errors[field].message;
                  break;
              default:
                  break;
          }
      }
  }
  
  router.get('/:id', (req, res) =&gt; {
      Employee.findById(req.params.id, (err, doc) =&gt; {
          if (!err) {
              res.render("employee/addOrEdit", {
                  viewTitle: "Update Module",
                  employee: doc
              });
          }
      });
  });
  
  router.get('/delete/:id', (req, res) =&gt; {
      Employee.findByIdAndRemove(req.params.id, (err, doc) =&gt; {
          if (!err) {
              res.redirect('/employee/list');
          }
          else { console.log('Error in module delete :' + err); }
      });
  });
  
  //post route for adding new task 
  router.post("/addtask", function(req, res) {
      var newTask = req.body.newtask;
      //add the new task from the post route
      task.push(newTask);
      res.redirect("/");
  });
  
  router.post("/removetask", function(req, res) {
      var completeTask = req.body.check;
      //check for the "typeof" the different completed task, then add into the complete task
      if (typeof completeTask === "string") {
          complete.push(completeTask);
          //check if the completed task already exits in the task when checked, then remove it
          task.splice(task.indexOf(completeTask), 1);
      } else if (typeof completeTask === "object") {
          for (var i = 0; i &lt; completeTask.length; i++) {
              complete.push(completeTask[i]);
              task.splice(task.indexOf(completeTask[i]), 1);
          }
      }
      res.redirect("/");
  });
  
  module.exports = router;
  </code></pre>