ExpressJS、Nodester、模板渲染路径?
我编写了一个简单的expressjs应用程序,它在本地运行得很好,甚至在no.de上也运行得很好,但是当我访问 http:// /myapp.nodester.com/ 我收到一个内部服务器错误
,日志显示
Error: failed to locate view "home"
at Function.compile (/node_modules/express/lib/view.js:58:15)
at ServerResponse._render (/node_modules/express/lib/view.js:416:18)
at ServerResponse.render (/node_modules/express/lib/view.js:317:17)
at Router.<anonymous> (/app/server.js:32:6)
at done (/node_modules/express/lib/router/index.js:250:22)
at middleware (/node_modules/express/lib/router/index.js:244:9)
at param (/node_modules/express/lib/router/index.js:227:11)
at pass (/node_modules/express/lib/router/index.js:232:6)
at Router._dispatch (/node_modules/express/lib/router/index.js:255:4)
at Object.handle (/node_modules/express/lib/router/index.js:45:10)
Error: failed to locate view "home"
at Function.compile (/node_modules/express/lib/view.js:58:15)
at ServerResponse._render (/node_modules/express/lib/view.js:416:18)
at ServerResponse.render (/node_modules/express/lib/view.js:317:17)
at Router.<anonymous> (/app/server.js:32:6)
at done (/node_modules/express/lib/router/index.js:250:22)
at middleware (/node_modules/express/lib/router/index.js:244:9)
at param (/node_modules/express/lib/router/index.js:227:11)
at pass (/node_modules/express/lib/router/index.js:232:6)
at Router._dispatch (/node_modules/express/lib/router/index.js:255:4)
at Object.handle (/node_modules/express/lib/router/index.js:45:10)
然后通过 fs 模块我试图查看为什么它不存在,并且出现fs.readdir('.',function(error,files){console.log(files)})
返回了这个:
[ '.nodester',
'app',
'node_modules',
'usr',
'lib64',
'sbin',
'etc',
'dev',
'proc',
'home',
'bin',
'lib',
'root',
'.PKGINFO',
'.INSTALL' ]
我还发现我 git 推送的所有文件
code>ed 位于 apps
目录内。
这是否意味着我必须告诉 Express 我的 views
文件夹所在的正确路径?我该怎么做? (我只知道如何告诉它在绝对路径中渲染文件)。
I wrote a simple expressjs app that runs just fine locally, and even on no.de, but on Nodester when I access http://myapp.nodester.com/ I get an Internal Server Error
and the logs say
Error: failed to locate view "home"
at Function.compile (/node_modules/express/lib/view.js:58:15)
at ServerResponse._render (/node_modules/express/lib/view.js:416:18)
at ServerResponse.render (/node_modules/express/lib/view.js:317:17)
at Router.<anonymous> (/app/server.js:32:6)
at done (/node_modules/express/lib/router/index.js:250:22)
at middleware (/node_modules/express/lib/router/index.js:244:9)
at param (/node_modules/express/lib/router/index.js:227:11)
at pass (/node_modules/express/lib/router/index.js:232:6)
at Router._dispatch (/node_modules/express/lib/router/index.js:255:4)
at Object.handle (/node_modules/express/lib/router/index.js:45:10)
Error: failed to locate view "home"
at Function.compile (/node_modules/express/lib/view.js:58:15)
at ServerResponse._render (/node_modules/express/lib/view.js:416:18)
at ServerResponse.render (/node_modules/express/lib/view.js:317:17)
at Router.<anonymous> (/app/server.js:32:6)
at done (/node_modules/express/lib/router/index.js:250:22)
at middleware (/node_modules/express/lib/router/index.js:244:9)
at param (/node_modules/express/lib/router/index.js:227:11)
at pass (/node_modules/express/lib/router/index.js:232:6)
at Router._dispatch (/node_modules/express/lib/router/index.js:255:4)
at Object.handle (/node_modules/express/lib/router/index.js:45:10)
Then through the fs module I tried to see why it was not there, and an fs.readdir('.',function(error,files){console.log(files)})
returned this:
[ '.nodester',
'app',
'node_modules',
'usr',
'lib64',
'sbin',
'etc',
'dev',
'proc',
'home',
'bin',
'lib',
'root',
'.PKGINFO',
'.INSTALL' ]
And I also found out that all the files I git push
ed are inside the apps
directory.
Does this mean I must tell express the right path where my views
folder is ? How can I do this ? (I only know how to tell it to render a file in an absolute path).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用内置变量 __dirname 手动设置视图目录:
尝试使用 Express 命令行工具生成应用程序框架(如果您从 npm 安装最新的 Express 版本,则可以访问)。它为您提供了良好的起点。
Setup views dir manually using built-in variable
__dirname
:Try to generate application skeleton using
express
comman-line tool (it's accessible, if you install last express version from npm). It generates good starting point for you.