【发布时间】:2012-04-30 06:40:22
【问题描述】:
注意:我在帖子末尾的自动回复
我正在尝试为 nodeJS 提供更好的体验,但我真的不喜欢将所有脚本放在一个文件中。
所以,我在这里的帖子之后使用这个结构
./
config/
enviroment.js
routes.js
public/
css/
styles.css
images
views
index
index.jade
section
index.jade
layout.jade
app.js
我的文件现在是:
app.js
var express = require('express');
var app = module.exports = express.createServer();
require('./config/enviroment.js')(app, express);
require('./config/routes.js')(app);
app.listen(3000);
enviroment.js
module.exports = function(app, express) {
app.configure(function() {
app.use(express.logger());
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade'); //extension of views
});
//development configuration
app.configure('development', function() {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
//production configuration
app.configure('production', function() {
app.use(express.errorHandler());
});
};
routes.js
module.exports = function(app) {
app.get(['/','/index', '/inicio'], function(req, res) {
res.render('index/index');
});
app.get('/test', function(req, res) {
//res.render('index/index');
});
};
layout.jade
!!! 5
html
head
link(rel='stylesheet', href='/css/style.css')
title Express + Jade
body
#main
h1 Content goes here
#container!= body
index/index.jade
h1 algoa
我得到的错误是:
错误:无法查找视图“索引/索引” 在 Function.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\application.js:495:17) 在渲染 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:614:9) 在 ServerResponse.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:638:5) 在 c:\xampp\htdocs\nodejs\buses\config\routes.js:4:7 在回调(c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:177:11) 在参数处(c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:151:11) 通过 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:158:5) 在 Router._dispatch (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:185:4) 在 Object.router [作为句柄] (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:45:10) 在下一个 (c:\xampp\htdocs\nodejs\buses\node_modules\express\node_modules\connect\lib\proto.js:191:15)
但我真的不知道是什么问题......
我开始想是因为模块导出...
答案: 我发现的唯一解决方案是更改我定义 app.set('views') 和视图引擎的位置
我将其移至 app.js,现在运行良好。
var express = require('express');
var app = module.exports = express.createServer();
require('./config/enviroment.js')(app, express);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
require('./config/routes.js')(app);
app.listen(3000);
我不太明白这背后的逻辑,但我想它有一个。
【问题讨论】:
-
我想你还在使用 express 2.x,因为 3.x 中的情况有点不同
-
可能是这样。我正在关注expressjs.com 的指南,但我没有看到任何与 express3 相关的内容:s
-
是的,它仍处于 alpha 阶段。如果你最近安装了 npm,你应该拥有它(3.0.0alpha)。最好发
npm ls看看你有什么版本。 -
对不起,我忘了补充,我有 3.0.0alpha1 有什么方法可以使用 v2,或者,如果最好获取任何文档?
-
当问题的自动答案优于接受的答案时...