ExpressJS、Nodester、模板渲染路径?

发布于 2024-12-02 10:59:52 字数 2084 浏览 3 评论 0原文

我编写了一个简单的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 pushed 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-12-09 10:59:52

使用内置变量 __dirname 手动设置视图目录:

var app = module.exports = express.createServer();
app.configure(function(){
        ...
        app.set('views', __dirname + '/views');
        app.use(express.static(__dirname + '/public'));
});

尝试使用 Express 命令行工具生成应用程序框架(如果您从 npm 安装最新的 Express 版本,则可以访问)。它为您提供了良好的起点。

Setup views dir manually using built-in variable __dirname:

var app = module.exports = express.createServer();
app.configure(function(){
        ...
        app.set('views', __dirname + '/views');
        app.use(express.static(__dirname + '/public'));
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文