[英]Cannot load jade view using express
我正在嘗試創建一個翡翠視圖並使用express加載它。 路徑/
正確加載,但是當我加載helloworld
,瀏覽器顯示Cannot get /helloworld
。
我創建了以下視圖並將其保存到視圖中:
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 content
,將其添加到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 | 阻止內容
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
空白頁面。這是我當前的layout.jade
:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
用Tab
four spaces
縮進Sublime Text
縮進。
如評論中所述, extend layout
應該是extends layout
。
僅供參考,如果您有權訪問服務器日志,則應在開發環境中顯示。
我知道這個問題已經有8個月沒有得到解答了,但是對於仍在尋求幫助的人,我相信問題在於:
res.render('helloworld',{title:'Hello World!'});
應該讀
res.render('index',{title:'Hello World!'});
因為接下來將利用索引路由器。
祝好運!
在/routes/index.js
更改以下內容:
exports.helloworld=function(req,res){
res.render('helloworld',{title:'Hello World!'});
};
對此:
exports.helloworld=function(req,res){
res.render('index',{title:'Hello World!'});
};
會像魅力一樣運作!
要不然...
在views文件夾中創建一個helloworld.jade
文件。 這是更推薦。
兩種解決方案在技術上都是正確的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.