运行MEAN应用程序时出现“没有默认引擎”错误

时间:2018-09-02 06:24:41

标签: node.js express mean-stack

错误:无法在视图目录“ / var / www / html / mean / mean-secure1 / views”中查找视图“错误”     在Function.render(/var/www/html/mean/mean-secure1/node_modules/express/lib/application.js:581:17)     在ServerResponse.render(/var/www/html/mean/mean-secure1/node_modules/express/lib/response.js:1008:7)     在/var/www/html/mean/mean-secure1/app.js:50:7     在Layer.handle_error(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/layer.js:71:5)     在trim_prefix(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:315:13)     在/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:284:7     在Function.process_params(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:335:12)     在下一个(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:275:10)     在/var/www/html/mean/mean-secure1/app.js:39:3     在Layer.handle上[作为handle_request](/var/www/html/mean/mean-secure1/node_modules/express/lib/router/layer.js:95:5)     在trim_prefix(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:317:13)     在/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:284:7     在Function.process_params(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:335:12)     在下一个(/var/www/html/mean/mean-secure1/node_modules/express/lib/router/index.js:275:10)     在SendStream.error(/var/www/html/mean/mean-secure1/node_modules/serve-static/index.js:121:7)     在SendStream.emit(events.js:182:13)

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('./config/database');

var api = require('./routes/api');
var app = express();
//app.set('view engine', 'pug');

mongoose.Promise = require('bluebird');
mongoose.connect(config.database, { promiseLibrary: require('bluebird') })
  .then(() =>  console.log('connection succesful'))
  .catch((err) => console.error(err));

app.use(passport.initialize());

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({'extended':'false'}));
app.use(express.static(path.join(__dirname, 'dist')));
app.use('/books', express.static(path.join(__dirname, 'dist')));
app.use('/api', api);
//app.use(express.static(path.join(_dirname, 'public')));

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

1 个答案:

答案 0 :(得分:0)

如果您想使用 ejs 模板作为视图引擎,则可以使用以下代码为节点应用程序设置视图引擎

const ejs=require("ejs");    
app.set('view engine','ejs');