Multiple view directories for a single expressjs application

i basically has an expressjs application with the following dirs:

app/views 

and

 app/plugin/views 

and would like to be able to serve both directories.

how will it work?

I tried to install

 app.set('views', second_dir) 

but then my application will not find my first directory of views.

then I tried symlinking the folder,

 fs.exists(viewDir, function() { fs.symlink(viewDir, rootViewDir, 'dir', function(err) { console.log('adding symlink with err: '+err+ ' in viewdir ='+viewDir+' rootDir ='+rootViewDir); }); }); 

this works, creates a symbolic link (constantly), but node cannot read symbolic representations as it seems.

is there a solution to my dilemma?

Thanks a lot, have fun Jascha

+8
express
source share
3 answers

found this video tj holowaychuk where he simply creates additional express applications for plugins. http://vimeo.com/56166857

he also specifically mentions the inheritance of "global" variables in the video comments, this means that app.locals and the like should be the same in every express application in the application and plugin chain, this will be checked now :)

anyway, I just wanted to answer my question with the answer, which I initially rejected without testing (because I did not assume that this global connection of variables exists;))

enjoy

Jascha

+2
source share

Why not change the way you structure your views so that the plugin folder is under your views folder? Then, when you visualize your views, you can specify a subpath at this time:

 res.render('plugin/pluginview1', ...); 
+3
source share

I am running Node.JS v0.8.20, Express.JS v4.2.0 and EJS v0.8.8 I have a path to 'views' defined in app.js as:

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

I defined the folder structure of the views as \views\{model}\{operations} . For example: \views\products\add.ejs .

When I refer to a view in a route file, for example, in \routes\products.js , I refer to a subdirectory and remove the backslash. For example, to reference the views\products\add.ejs , I use:

 res.render('products\\add'); 

I like this format in that it allows me to save model operations in representations, one per file, and then group operations for one model object in folders.

+1
source share

All Articles