Search code examples
expressexpress-handlebars

Express handlebars wont render data


Hey guys so I have the following in my app.js file

const express = require("express");
const app = express();
const path = require('path');
const hbs = require("express-handlebars");

    app.engine(
      "hbs",
      hbs({
        extname: "hbs",
        //defaultLayout: "layout",
        layoutsDir: __dirname + "/views/layouts/"
      })
    );
    app.set("views", path.join(__dirname, "views"));
    app.set("view engine", "hbs");

I also have a file located in server/views/layouts/ that is a handle bars file called share.hbs I also have a file in my server/api/media.js that looks like this

const express = require("express");
const router = express.Router();
const database = require("../db");
router.get("/share", async (req, res, next) => {
  res.render("share");
});

But when ever I try to render the file it errors out with the following error

enter image description here

enter image description here


Solution

  • Had to move my file out of the layouts folder and add this to the res.render call

      res.render("share.hbs", { layout: "share" });