gpt4 book ai didi

javascript - 无法从命令行在 Node 上安装 Handlebars

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

我一直在尝试让 Handlebars 与 Node 一起使用。我的书指示我安装这样的 Handlebars :npm install --save express3-handlebar。这引发了错误

npm WARN 已弃用 express3-handlebars @0.5.2:此包已重命名为:express-handlebars

所以我尝试了 npm install --save express-handlebar

当我尝试启动服务器node meadowlark.js时,命令提示符显示Express started on....

但是当我将 localhost 放入浏览器时,我收到以下信息:

Error: No default engine was specified and no extension was provided.
at new View (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\view.js:48:42)
at EventEmitter.app.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\application.js:509:12)
at ServerResponse.res.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\response.js:904:7)
at require.create.defaultLayout (C:\Users\myUserName\Desktop\project\meadowlark\site\meadowlark.js:20:6)
at Layer.handle_error (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\layer.js:58:5)
at trim_prefix (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:269:13)
at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:238:9
at Function.proto.process_params (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:313:12)
at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:229:12
at Function.match_layer (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:296:3)

然后我注意到在我的索引文件中我仍然有

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');

所以我将其更改为从第三行中删除 3。仍然是同样长的错误行。我做错了什么来收到这个错误?安装 Handlebars 时我不在正确的目录中吗?我已经从我的应用程序根目录以及其中包含我的 index.js

的文件中尝试过它
var express = require('express');
var app = express();
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.');
});

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');

我正在使用的示例来自 Web Development with Node & Express (伊桑·布朗)。

最佳答案

我在您的代码中看到的主要问题是您正在创建 app 的两个实例:

var app = express(); // I see this line twice in your code

这意味着您在应用程序的错误实例上配置了 Handlebars ,因此 “未指定默认引擎”

如果您删除第二个实例,您应该是黄金。这是我将如何重写该代码的示例:

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

app.set('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.');
});

您可以在 require 语句中使用和不使用 3 进行尝试,但它应该可以工作。

希望对你有帮助!!!

关于javascript - 无法从命令行在 Node 上安装 Handlebars ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26854207/

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