gpt4 book ai didi

node.js - 查找 View 失败

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:59 25 4
gpt4 key购买 nike

您好,我正在尝试使用 Handlebars 来使用 Express 3。但我无法“查找 View ”,我陷入了这个错误。

Error: Failed to lookup view "500" in views directory
"d:\projects\meadowlark\site\views" at EventEmitter.app.render
(d:\projects\meadowlark\site\node_modules\express\lib\application.js:519:17)
at ServerResponse.res.render
(d:\projects\meadowlark\site\node_modules\express\lib\response.js:904:7)
at d:\projects\meadowlark\site\meadowlark.js:29:7 at
Layer.handle_error
(d:\projects\meadowlark\site\node_modules\express\lib\router\layer.js:58:5)
at trim_prefix
(d:\projects\meadowlark\site\node_modules\express\lib\router\index.js:269:13)
at
d:\projects\meadowlark\site\node_modules\express\lib\router\index.js:238:9
at Function.proto.process_params
(d:\projects\meadowlark\site\node_modules\express\lib\router\index.js:313:12)
at
d:\projects\meadowlark\site\node_modules\express\lib\router\index.js:229:12
at Function.match_layer
(d:\projects\meadowlark\site\node_modules\express\lib\router\index.js:296:3)
at next
(d:\projects\meadowlark\site\node_modules\express\lib\router\index.js:190:10)

这对我来说真的很奇怪。谁能向我解释一下这个问题我该如何解决?

我的js代码如下:

var express = require('express');
var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port',process.env.port || 3000);

app.get('/', function(req, res) {
res.render('home');
});

app.get('/about', function(req, res) {
res.render('about');
});

// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});

// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});

app.listen(app.get('port'),function(){
console.log('Express started on http://localhost: '+ app.get('port') + " Press CTRL+C to terminate.");
});

最佳答案

我想我知道这里的问题是什么,记住 O'Reilly 书中的 app.js 设置(“使用 Node & Express 进行 Web 开发”)。以下是作者未能澄清的内容:

如果您有 .handlebars模板文件位于 views/layouts/ ,像这样构建您的 View 引擎设置:

var path = require('path');
var express = require('express');
var handlebars = require('express-handlebars'); // 'express3-handlebars' has been deprecated
var app = express();

// Set up handlebars view engine
app.set('views', path.join(__dirname, 'views/layouts/'));
app.engine('handlebars', handlebars({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

请注意,您将需要 var path = require('path');为了将“ View ”设置为文件路径 views/layouts/ .

关于node.js - 查找 View 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27763846/

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