2

According to the Express Docs, I can use <Application>.set("views", "my/views/path"); to set a generic directory to store my views in. I've set up my project so that each of my routes has its own directory with its own views (and other route-specific content) to compliment my sub-domain based approach. I would like to be able to do something like <Router>.set("views", "router/views/");, so that, when I want to render a page, I can simply call res.render("homepage"); instead of res.render("<router>/views/homepage"); for that specific router.

Right now, I have the following setup in my root app.js:

app.set("views", join(__dirname, "domains"));
app.set("view engine", "ejs");
// ...
for (const d in domains)
{
    if (d === "root")
        continue;

    const router = domains[d];
    app.use(vhost(`${d}.${process.env.DOMAIN_ROOT}`, router));
}

In my "blog" router (for my blog.website.com sub-domain), I have:

router.get("/", function(req, res)
{
    res.render("blog/views/index", {
        title: "My Tech Blog"
    });
});

I would like to be able to specify a "views" directory for each router, something along the lines of router.set("views");

Medallyon
  • 110
  • 1
  • 1
  • 15

1 Answers1

2

I'm going with the sub-app pattern suggested by this thread.

I changed the line at the top of my router file from:

const router = require("express").Router();

to

const router = require("express")();

And instead of using Routers for my projects, I simply make use of sub-apps to specify independent view paths:

router.set("view engine", "ejs");
router.set("views", join(__dirname, "domains"));
Medallyon
  • 110
  • 1
  • 1
  • 15