ExpressJS,Nodester,模板渲染路径?

时间:2011-08-31 15:28:49

标签: node.js express pug

我写了一个简单的expressjs应用程序,在本地运行得很好,甚至在no.de上运行,但在我访问http://myapp.nodester.com/时在Nodester上我得到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文件夹所在的正确路径?我怎样才能做到这一点 ? (我只知道如何告诉它在绝对路径中渲染文件)。

1 个答案:

答案 0 :(得分:2)

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

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

尝试使用express comman-line工具生成应用程序框架(如果从npm安装最后一个快速版本,则可以访问它)。它为你创造了良好的起点。