你如何在一个快速的应用程序设置玉basedir选项? (“basedir”选项需要使用“扩展”和“绝对”path)

通过peepcode nodejsvideo并在当前的express / node版本上重新创build应用程序,我遇到了一个小问题。

文件结构

site - apps - - authentication - - - views - - - - login.jade - - - routes.js - node_modules - public - - images - - javascripts - - stylesheets - routes - views app.js 

login.jade:

 extends /views/layout block content form(action='/sessions', method='post') label | Username input(type='text', name='user') label | Password input(type='password', name='password') input(type='submit', name='submit') 

app.js

 var express = require('express') , user = require('./routes/user') , http = require('http') , path = require('path'); var app = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set('view options', { basedir: process.env.__dirname}) app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); require('./apps/authentication/routes')(app) http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); 

当我导航到localhost:3000/login收到以下错误:

 Error: /Users/bob/code/site/apps/authentication/views/login.jade:1 > 1| extends /views/layout 2| block content 3| form(action='/sessions', method='post') 4| label the "basedir" option is required to use "extends" with "absolute" paths 

我可以更新延长线:扩展../../../views/layout,并使其工作,但我更想知道如何设置basedir选项。

       

网上收集的解决方案 "你如何在一个快速的应用程序设置玉basedir选项? (“basedir”选项需要使用“扩展”和“绝对”path)"

你可以使用这个:

 app.locals.basedir = '/your/base/directory'; 

或者,使用更新的Express方法,

 app.locals.basedir = path.join(__dirname, 'views'); 

快递中,您会注意到,

 app.set('views', path.join(__dirname, 'views')); 

那么你可以把这条线放在那里,

 app.locals.basedir = app.get('views'); 

这使得basedir可用于jade.js因此它可以parsing绝对path。