1

我写了一个简单的 expressjs 应用程序,它在本地运行得很好,甚至在 no.de 上也能正常运行,但是在 Nodester 上,当我访问http://myapp.nodester.com/时,我得到一个Internal Server Error并且日志说

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 push编辑的所有文件都在apps目录中。

这是否意味着我必须告诉我views文件夹所在的正确路径?我怎样才能做到这一点 ?(我只知道如何告诉它以绝对路径呈现文件)。

4

1 回答 1

2

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

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

尝试使用命令express行工具生成应用程序骨架(如果您从 npm 安装最后一个 express 版本,它是可访问的)。它为您提供了良好的起点。

于 2011-09-01T12:52:31.540 回答