node.js 为什么 express 告诉我我的默认视图引擎未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17560760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:59:41  来源:igfitidea点击:

Why is express telling me that my default view engine is not defined?

node.jsmongodbexpressmongooseejs

提问by Christian Grabowski

I'm using nodejs and mongodb in the back end for an app I'm working on. I'm using express to test the app, and I'm trying to use ejs to render my html files. However, I'm having the issue of my default view engine not being defined.

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

Here is my app.js:

这是我的 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'));
});

Here is my package.json:

这是我的 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"
  }
}

ERRORS:

错误:

Express 500 Error: Failed to lookup view "index"

Express 500 错误:无法查找视图“索引”

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)

Now when I try to run it my terminal outputs:

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

/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)

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Jonathan Lonowski

The source of the errordescribes the requirements:

错误来源描述了要求:

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

Express expects that you either specify the view with its extension:

Express 期望您使用其扩展名指定视图:

res.render('index.html');

Or specify a default view engineand name your views after it:

或者指定一个默认的视图引擎并用它来命名你的视图:

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

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


Regarding your edit:

关于您的编辑:

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

The issue is with this line:

问题出在这一行:

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

As the documentation demonstrates, app.engine()is expecting a functionreference. You can do this by simply removing the ()that callrenderFile:

正如文档所示app.engine()期待function参考。您可以通过简单地删除该调用来做到这()一点:renderFile

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

回答by Loourr

need to do all the app.setand app.usein an app.configure

需要做所有的app.setapp.use在一个app.configure

try this

尝试这个

app.configure(function(){ 
    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());
    }
});