简体   繁体   English

车把的 Express.JS 路径问题

[英]Express.JS path issue with handlebars

我的文件夹结构

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). “app”文件夹是我的节点逻辑所在的位置,您可以在那里找到views文件夹,其中包含把手(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:在“config”文件夹中,有一个 express.js 文件,其中实际上需要 express 包,也是我设置应用程序引擎和变量的位置:

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. server.js 文件需要来自 config 文件夹的 express.js 文件,就是这样。

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.. "如果我将“views”文件夹放在根目录上(第 2 行被排除在外),一切正常,但是,如果我使用第 2 行和图片中显示的文件夹结构,我总是收到错误 ENOENT:“没有这样的文件或目录..”

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).在尝试通知“views”文件夹的正确路径时,我尝试在第 2 行使用什么格式并不重要,错误消息总是说找不到文件,并且显示的 URL 总是错过“应用程序”文件夹(它显示了我尝试的每个路径,但它使应用程序部分消失了)。 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.我做什么并不重要,但 Node 似乎只是忽略了那个 app 文件夹..就好像我无法访问它一样。

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.例如:当我在第 2 行只使用“__dirname”时,我得到的错误消息是节点“无法在视图目录中查找视图“文件名”:“path_string/config”……我很难解释它更好(我是新手),但我想你们比我更有经验,可能会理解我在说什么。

Thanks in advance for your help.在此先感谢您的帮助。

Vittorio维托里奥

Solved: The issue was with express-handlebars package.已解决:问题与 express-handlebars 包有关。 After using it's "create" method I should've specified layoutsDir:使用它的“创建”方法后,我应该指定 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再见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM