Heroku 找不到 Handlebars Partials
Heroku not Finding Handlebars Partials
我完全迷失了这个问题,我敢肯定我现在一定是瞎了,但我无法解决它。 Heroku 正在为确实存在的部分返回一个部分无法找到的错误(如下)。
注意:这一切都在本地主机上完美运行。
正在使用
- 节点:10.5.0
- 表示:4.16.3
- 血红蛋白:4.0.1
文件结构:
| > Server
| > server.js
| > Views
| > layout.hbs
| > Partials
| > pageheader.hbs
server/server.js
let app = express();
hbs.registerPartials(__dirname + '\..\views\partials');
app.set('view engine', 'hbs');
app.get('/', (req, res) => {
res.render('layout.hbs', {
title: 'Home'
}
});
views/layout.hbs
...
<body>
{{> pageheader}}
</body>
...
views/partials/pageheader.hbs
<h1>{{title}}</h1>
最后...
Heroku 日志
/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266
throw new _exception2['default']('The partial ' + options.name + ' could not be found');
^
Error: The partial pageheader could not be found
at Object.invokePartial (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266:11)
at Object.invokePartialWrapper [as invokePartial] (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:68:39)
at Object.eval [as main] (eval at createFunctionContext (/app/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:12:28)
at main (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
at ret (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
at ret (/app/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
at render_file (/app/node_modules/hbs/lib/hbs.js:49:23)
at render_with_layout (/app/node_modules/hbs/lib/hbs.js:80:5)
at cacheAndCompile (/app/node_modules/hbs/lib/hbs.js:151:5)
at /app/node_modules/hbs/lib/hbs.js:171:7
at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)
感谢任何帮助/建议,我搜索了 google,阅读了所有我能找到的似乎相关的内容。我只是不明白为什么这适用于本地主机而不适用于 Heroku。
我认为文件路径有误,请尝试更改
__dirname + '\..\views\partials'
至
__dirname + '/../views/partials'
所以...我想我发现问题是路径的“..”部分。
我已将 server.js 移动到根目录并更改了文件引用以匹配该更改...现在它在任何地方都可以正常工作。
我完全迷失了这个问题,我敢肯定我现在一定是瞎了,但我无法解决它。 Heroku 正在为确实存在的部分返回一个部分无法找到的错误(如下)。
注意:这一切都在本地主机上完美运行。
正在使用
- 节点:10.5.0
- 表示:4.16.3
- 血红蛋白:4.0.1
文件结构:
| > Server
| > server.js
| > Views
| > layout.hbs
| > Partials
| > pageheader.hbs
server/server.js
let app = express();
hbs.registerPartials(__dirname + '\..\views\partials');
app.set('view engine', 'hbs');
app.get('/', (req, res) => {
res.render('layout.hbs', {
title: 'Home'
}
});
views/layout.hbs
...
<body>
{{> pageheader}}
</body>
...
views/partials/pageheader.hbs
<h1>{{title}}</h1>
最后...
Heroku 日志
/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266
throw new _exception2['default']('The partial ' + options.name + ' could not be found');
^
Error: The partial pageheader could not be found
at Object.invokePartial (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266:11)
at Object.invokePartialWrapper [as invokePartial] (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:68:39)
at Object.eval [as main] (eval at createFunctionContext (/app/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:12:28)
at main (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
at ret (/app/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
at ret (/app/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
at render_file (/app/node_modules/hbs/lib/hbs.js:49:23)
at render_with_layout (/app/node_modules/hbs/lib/hbs.js:80:5)
at cacheAndCompile (/app/node_modules/hbs/lib/hbs.js:151:5)
at /app/node_modules/hbs/lib/hbs.js:171:7
at FSReqWrap.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:53:3)
感谢任何帮助/建议,我搜索了 google,阅读了所有我能找到的似乎相关的内容。我只是不明白为什么这适用于本地主机而不适用于 Heroku。
我认为文件路径有误,请尝试更改
__dirname + '\..\views\partials'
至
__dirname + '/../views/partials'
所以...我想我发现问题是路径的“..”部分。
我已将 server.js 移动到根目录并更改了文件引用以匹配该更改...现在它在任何地方都可以正常工作。