将ejs与expressjs一起使用时出现内部服务器错误

时间:2018-05-03 07:30:38

标签: javascript node.js express ejs

我是nodejs的新手,我试图使用ejs模板化我的网站。但是当我尝试设置视图引擎时,我不断收到错误代码500 - 内部服务器错误。这是我的app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var engines = require('consolidate');

var index = require('./routes/index');
var main = require('./routes/main');
var suoimo = require('./routes/suoimo');
var vungtau = require('./routes/vungtau');
var video = require('./routes/video');
var dalat = require('./routes/dalat');

var app = express();

app.use(express.static('./'));

app.use(express.static('./public'));

// view engine setup
// app.set('views', path.join(__dirname, 'views'));
// app.engine('html', engines.mustache);
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// app.use('/', index);
app.use('/video', video);
app.use('/', index);
app.use('/suoimo', suoimo);
app.use('/vungtau', vungtau);
app.use('/main', main);
app.use('/dalat', dalat);

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

但是如果我像这样设置视图和视图引擎:

app.set('views', path.join(__dirname, 'views'));
app.engine('html', engines.mustache);
app.set('view engine', 'html');

一切正常,有谁知道为什么?请帮忙

1 个答案:

答案 0 :(得分:0)

测试了您的代码一切正常。但如果我卸载 ejs,我会收到错误500内部服务器错误。我认为这是你的问题。 尝试运行 npm install ejs并使用相同的配置重试,现在应该可以正常工作。