MattDionis
MattDionis

Reputation: 3616

Possible to use both multiple view engines and multiple view folders in Express?

I'm working on a project that currently relies on Jade templates and uses the following setup:

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

I'm building a 'V2' version of the site and will be using client-side HTML rather than Jade. I setup a basic workaround to avoid the current Jade structure by doing the following in my route handler:

function(req, res) {
  res.sendFile(path.join(__dirname, '../assets/v2', 'index.html'));
};

The problem is that I now also need to send req.user details along with rendering the page, and obviously res.sendFile() does not allow this. Ideally I would like to do something like this in my route handler:

function(req, res) {
  res.render(path.join(__dirname, '../assets/v2', 'index.html'), {user: req.user});
};

Upvotes: 0

Views: 152

Answers (2)

xyz
xyz

Reputation: 3409

You would be writing a lot of redundant code if you want to serve dynamic html without any templating engine. Express doesn't offer any functionality to do this by itself, and it shouldn't.

What you can do otherwise is build a REST API, use MVC on client side (angular.js is pretty good) and serve data.

For eg:

GET /user-data

{
    user: some_user
}

Upvotes: 1

Wayne Bloss
Wayne Bloss

Reputation: 5550

Take your index.html file and make it a index.jade template that puts the user info wherever you want.

The browser will receive it as an HTML file, not a jade template.

Upvotes: 0

Related Questions