2

my folder structure

The thing is. the "app" folder is where my node's logic sits and there you can find the views folder, with templates for handlebars (express-handlebars).

On the "config" folder, there's an express.js file where express package is actually required and also where I set the app engine and variables:

line 1: app.engine('handlebars', handlebars.engine);
line 2: app.set('views', path.join(__dirname, '../app/views');
line 3: app.set('view engine', 'handlebars');

The server.js file requires the express.js file from config folder and that's it.

If I put the "views" folder on the root (and line 2 is excluded), everything works fine, however, if I use line 2 and the folder structure shown in the picture, I always get an error ENOENT: "no such file or directory.. "

It doesn't matter what format I try to use on line 2 when trying to inform the correct path for the "views" folder, the error message always says the file can't be found and the URL shown ALWAYS misses the "app" folder (it shows every path I try, but it makes the app portion vanish). It doesn't matter what I do, but Node seems to simply ignore that app folder.. it's just as if I can't access it.

Ex:
1) path.join(__dirname, "../app/views"); > ENOENT "C:\root\views\layouts\main.handlebars".
2) path.join(__dirname, "../xapp/views"); > Failed lookup view "C:\root\xapp\views"

Note how the "app" portion of the first option disapears from the url !!

I'm assuming this is a "path" issue and I can't understand what's going on. Could somebody help me out on that, please?

In case someone knows where I could read about the logic behind this "path thing" I'd be greatful. For example: When i use only "__dirname" on line 2, the error message I get is that node "failed to lookup view "filename" in views directory: "path_string/config"... it's kind of hard for me to explain it better (I'm a newbie), but I figure u guys, much more experienced than I am, might understand what I'm talking about.

Thanks in advance for your help.

Vittorio

  • Does something like `app.set('views', path.join(__dirname, '../app/views');` work? – Ash Mar 22 '16 at 16:53
  • Thanks Steve, but no, it doesn't. I have changed my "app" folder's name to "mapp". This is what happens: A) path.join(__dirname, '../app/views) > Failed to lookup view... 'C:\web_app\app\views' B) path.join(__dirname, '../mapp/views) > ENOENT ... 'C:\web_app\views\layouts\main.handlebars'.. note that on option B, the mapp directory is not shown in the error message: It should be 'C:\web_app\mapp\views\layouts\main.handlebars', but, instead, it says 'C:\web_app\views\layouts\main.handlebars'. > mapp disapeared. – Vittorio Machado Lo Bianco Mar 22 '16 at 17:41
  • shouldn't express be in the node_modules? – Amin Mohamed Ajani Mar 22 '16 at 18:34
  • It is.. but that doesn't make any difference because everything works just fine if i create the "views" folder on the root folder. The problem occurs only when the "views" folder is on the "app" folder. – Vittorio Machado Lo Bianco Mar 22 '16 at 19:00

1 Answers1

2

Solved: The issue was with express-handlebars package. After using it's "create" method I should've specified layoutsDir:

require('express-handlebars')
      .create({
               layoutsDir:path.join(__dirname, '../whateverpath/views/layouts') , 
               defaultLayout: 'main'
             });

After doing it, the problem was solved.

Thanks for those who tried to help.

Bye