gpt4 book ai didi

node.js - 为什么 express 告诉我我的默认 View 引擎没有定义?

转载 作者:IT老高 更新时间:2023-10-28 13:08:08 26 4
gpt4 key购买 nike

我在后端使用 nodejs 和 mongodb 来开发我正在开发的应用程序。我正在使用 express 来测试应用程序,并且我正在尝试使用 ejs 来呈现我的 html 文件。但是,我遇到了未定义默认 View 引擎的问题。

这是我的 app.js:

/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var conf = require('./conf');
var app = express();
var mongoose = require('mongoose');
, Schema = mongoose.Schema
, ObjectId = mongooseSchemaTypes.ObjectID;
var UserSchema = new Schema({})
, User;
// all environments
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
app.engine('.html', require('ejs').renderFile());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

这是我的 package.json:

{
"name": "application-name",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.3.3",
"ejs":">= 0.0.1",
"mongoose-auth": ">= 0.0.12",
"mongoose": ">=2.4.8",
"everyauth": ">=0.2.28"
}
}

错误:

express 500 错误:无法查找 View “索引”

at Function.app.render (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:494:17)
at ServerResponse.res.render (/home/christian/node_modules/nave/create/node_modules/express/lib/response.js:756:7)
at exports.index (/home/christian/node_modules/nave/create/routes/index.js:7:7)
at callbacks (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:161:37)
at param (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:135:11)
at pass (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:170:5)
at Object.router (/home/christian/node_modules/nave/create/node_modules/express/lib/router/index.js:33:10)
at next (/home/christian/node_modules/nave/create/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/home/christian/node_modules/nave/create/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:49:5)

现在当我尝试运行它时,我的终端输出:

/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:173
if ('function' != typeof fn) throw new Error('callback function required');
^
Error: callback function required
at Function.app.engine (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:173:38)
at Function.<anonymous> (/home/christian/node_modules/nave/create/app.js:26:9)
at Function.app.configure (/home/christian/node_modules/nave/create/node_modules/express/lib/application.js:392:61)
at Object.<anonymous> (/home/christian/node_modules/nave/create/app.js:23:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)

任何帮助将不胜感激。

最佳答案

source of the error描述要求:

if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');

Express 希望您指定带有扩展名的 View :

res.render('index.html');

或指定 default view engine并以此命名你的观点:

app.set('view engine', 'ejs');

// `res.render('index')` renders `index.ejs`

关于您的编辑:

if ('function' != typeof fn) throw new Error('callback function required');

问题在于这一行:

app.engine('.html', require('ejs').renderFile());

As the documentation demonstrates , app.engine() 需要一个 function 引用。您可以通过简单地删除 调用 renderFile() 来做到这一点:

app.engine('.html', require('ejs').renderFile);

关于node.js - 为什么 express 告诉我我的默认 View 引擎没有定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17560760/

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