我正在尝试创建一个 jade View 并使用 express 加载它。路径 /
加载正确,但是当我加载 helloworld
时,浏览器显示 Cannot get/helloworld
。
我创建了以下 View 并将其保存到 View 中:
extend layout
block content
h1=title
p Hello! Hello World! Welcome to #{title}
在 routes/index.js
中,我这样做了:
exports.helloworld=function(req,res){
res.render('helloworld',{title:'Hello World!'});
};
在 app.js
文件中:
app.get('/helloworld',routes.helloworld);
更新:
完整的 app.js
文件:
/**
* Module dependencies.
*/
var express = require('express')
,routes = require('./routes')
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.get('/helloworld',routes.helloworld);
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
我的routes.js
:
exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
exports.helloworld=function(req,res){
res.render('helloworld',{title:'Hello World!'});
};
layout.jade
没有 block 内容
,将其添加到layout.jade
而不是body!=body
导致此错误:
Error: /home/anr/Desktop/node js/withdb/views/layout.jade:7
5| link(rel='stylesheet', href='/stylesheets/style.css')
6| body
7| #content 8| block content
Invalid indentation, you can use tabs or spaces but not both
at Object.Lexer.indent (/home/anr/Desktop/node
js/withdb/node_modules/jade/lib/lexer.js:762:15)
at Object.Lexer.next (/home/anr/Desktop/node
js/withdb/node_modules/jade/lib/lexer.js:870:15)
at Object.Lexer.lookahead (/home/anr/Desktop/node
js/withdb/node_modules/jade/lib/lexer.js:114:46)
at Parser.lookahead (/home/anr/Desktop/node
js/withdb/node_modules/jade/lib/parser.js:100:23)
at Parser.peek (/home/anr/Desktop/node js/withdb/node_modules/jade/lib/parser.js:77:17)
at Parser.tag (/home/anr/Desktop/node js/withdb/node_modules/jade/lib/parser.js:733:22)
at Parser.parseTag (/home/anr/Desktop/node js/withdb/node_modules/jade/lib/parser.js:719:17)
at Parser.parseExpr (/home/anr/Desktop/node js/withdb/node_modules/jade/lib/parser.js:188:21)
at Parser.block (/home/anr/Desktop/node js/withdb/node_modules/jade/lib/parser.js:689:25)
at Parser.tag (/home/anr/Desktop/node js/withdb/node_modules/jade/lib/parser.js:806:26)
因此,使用空格缩进会导致加载空白页面。这是我当前的 layout.jade
:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
Sublime Text
为 Tab
缩进 四个空格
。
最佳答案
正如评论中所讨论的,
extends layout
应该是extends layout
。
仅供引用,如果您有权访问服务器日志,这应该会显示在开发环境中。
关于javascript - 无法使用 express 加载 Jade View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22255273/