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

最后都变了- 提交于 2019-11-30 11:30:19

The source of the error describes 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:

res.render('index.html');

Or specify a default view engine and 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');

The issue is with this line:

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

As the documentation demonstrates, app.engine() is expecting a function reference. You can do this by simply removing the () that call renderFile:

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

need to do all the app.set and app.use in an 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());
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!