gpt4 book ai didi

node.js - 如何在 express 应用程序中设置基于 Jade 的选项? (使用 "basedir"和 "extends"路径需要 "absolute"选项)

转载 作者:IT老高 更新时间:2023-10-28 23:00:56 25 4
gpt4 key购买 nike

浏览 peepcode nodejs 视频并在当前 express/node 版本上重新创建应用程序,我遇到了一个小问题。

文件结构

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

我可以将 extends 行更新为: extends ../../../views/layout 并使其工作,但我更想知道如何设置 basedir 选项。

最佳答案

你可以用这个:

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

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

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

关于node.js - 如何在 express 应用程序中设置基于 Jade 的选项? (使用 "basedir"和 "extends"路径需要 "absolute"选项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16525362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com