Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node/Express.js - Overriding where to look for the 'Views' folder for each request

Node/Express.js - Overriding where to look for the 'Views' folder for each request

Problem

In my Node/Express.js project I can set the Views Folder globally like so:

app.configure(function() {
    app.set('views', __dirname + '/views');
    .... snip ....
});

...and all my view templates go into the views Folder.

Is it possible to override where Express looks for views on a request by request basis? For instance, something like the following:

app.get('/', function(req, res) {
    res.render('index', { viewFolder: 'otherViews' });
});

The reason I ask is I'm trying to replicate Microsoft ASP.NET MVC's Areas functionality where each Area gets it's own views folder.

Problem courtesy of: Sunday Ironfoot

Solution

The 'views' setting is the root directory, so you should be able to specify a sub-folder in the hierarchy:

app.get('/', function(req, res) {
    res.render('areaName/viewName');
});

It means your 'areas' need to be sub-folders, but it allows you to accomplish the separation you are looking for.

Solution courtesy of: Dave

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node/Express.js - Overriding where to look for the 'Views' folder for each request

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×