Unable to view template engine using handlebars

Omar Martin
Omar Martin used Ask the Experts™
on
structure
I am attempting to render a view through the template engine "handlebars" for node.js with the express framework. Please see attached my file structure.  After running the route below, I keep receiving the following error: [Error: Failed to lookup view "layout" in views directory "C:\Users\frontstart\Dropbox\Web Design\nodetest\views"]. Can someone please help me locate my error.


router.get("/alldata", (req, res) =>
  Store.findAll()
    .then(stores => {
        path is //views/gigs.handlebars
        res.render('gigs', {
              stores
        });
       })
    .catch(err => console.log(err)));

module.exports = router;

Here is the backend app.js

const fs = require("fs");
const express = require("express");
const expressSession = require("express-session");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const path = require("path");
const expressLayouts = require("express-ejs-layouts");
const mysql = require("mysql");
const mysql2 = require("mysql2");
const multer = require("multer");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const Joi = require("@hapi/joi");
const connectFlash = require("connect-flash");
const morgan = require("morgan");
const passport = require("passport");
const passportLocal = require("passport-local");
const Sequelize = require("sequelize");
const db = require("./middleware/db.js");
const logger = require("./middleware/logger");
const tokenValidator = require("./middleware/tokenValidator");
const exphbs = require('express-handlebars');

const app = express();
app.use(bodyParser.json());
app.use(
	bodyParser.urlencoded({
		extended: false
	})
);

//Initialize Middleware..
app.use(cors());
app.use(expressLayouts);
app.use(logger);

//Handlebars view engine (Middleware) - (/views/layouts/main.handlebars)
app.engine('handlebars', exphbs({defaultLayout: 'main'}));

//put in 
///
app.set('view engine', 'handlebars');
//app.set('views', __dirname + '/../views');
//app.set('views', __dirname + '/views');

app.set('views', path.join(__dirname, 'views'));

//Database authentication.
db.authenticate()
  .then(() => {
	console.log('Connection has been established successfully..');
})
  .catch(err => {
	console.error('Unable to connect to the database:', err);
});

//
//app.get("/", (req, res) => res.send("INDEX"));
app.get("/", (req, res) => res.render("INDEX"));

//Routes
app.use("/api", require("./routes/api/stations"));

//Setting Static Folder
//app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname + '.../public')));

//Uploading Images Folder
app.use("/uploads", express.static("uploads"));

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bridgemaker
Commented:
app.use(expressLayouts); from previous ejs template to blocking the use of handlebars template....

I simply commented it out in app.js

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial