二次伤害:Express.js看不到错误页面。主要伤害:?新人的猫鼬错误?

时间:2015-07-02 19:22:11

标签: javascript node.js mongodb express mongoose

我在500 Error: Failed to lookup view "index" in views directory发现了一个类似的问题,但我认为我已经覆盖了扩展程序。

为了能够使用类似HTML的模板,我的app.js中有:

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

在views目录中,我有error.htmlerror.jade和零字节error文件。 error.html缺少有趣的格式,但包含:

<!DOCTYPE html>
<html>
  <head>
    <title>Error</title>
  </head>
  <body>
  <%= message =>
  <%= error.status =>
  <%= error.stack =>
  </body>
</html>

当我尝试访问页面以创建用户帐户时(通过在Mongoose中添加条目),我在尝试保存时出错:

Error: Failed to lookup view "error" in views directory "/Users/jonathan/server/views"
    at EventEmitter.app.render (/Users/jonathan/server/node_modules/express/lib/application.js:555:17)
    at ServerResponse.res.render (/Users/jonathan/server/node_modules/express/lib/response.js:938:7)
    at module.exports (/Users/jonathan/server/app.js:106:7)
...

这可能是在我尝试插入数据库失败后报告错误页面失败的次要损害。

我认为主要的伤害是猫鼬;我认为相关的代码是:

var schema = mongoose.Schema;
var user_details = new schema(
  {
  username: String,
  password: String
  },
  {
  collection: 'userInfo'
  });

passport.use(new local_strategy(function(username, password, done)
  {
  process.nextTick(function()
    {
    user_details.findOne(
      {
      'username': username,
      }, function(err, user)
      {
      if (err)
        {
        return done(err);
        }
      if (!user)
        {
        return done(null, false);
        }
      if (user.password !== password)
        {
        return done(null, false);
        }
      return done(null, user);
      });
    });
  }));

在我的routes / index.js中,POST到/ newuser的处理程序是:

router.post('/newuser', function(request, response, next)
  {
  var newuser = new user_details(
    {
    'username': request.params.username,
    'password': request.params.password
    });
  newuser.save();
  });

- UPDATE -

根据评论中的请求,这是我的app.js文件:

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

var routes = require('./routes/index');
var users = require('./routes/users');
var passport = require('passport');
var local_strategy = require('passport-local').Strategy;

var app = express();

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

// uncomment after placing your favicon in /public
app.use(favicon(__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(app.router);
app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(function(user, done)
  {
  done(null, user);
  });

passport.deserializeUser(function(user, done)
  {
  done(null, user);
  });

var schema = mongoose.Schema;
var user_details = new schema(
  {
  username: String,
  password: String
  },
  {
  collection: 'userInfo'
  });

passport.use(new local_strategy(function(username, password, done)
  {
  process.nextTick(function()
    {
    user_details.findOne(
      {
      'username': username,
      }, function(err, user)
      {
      if (err)
        {
        return done(err);
        }
      if (!user)
        {
        return done(null, false);
        }
      if (user.password !== password)
        {
        return done(null, false);
        }
      return done(null, user);
      });
    });
  }));

app.use('/', routes);
app.use('/users', users);

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

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

var server = app.listen(8000, function()
  {
  console.log('Server running.');
  });

module.exports = app;

- 进一步更新 -

现在,在创建index.ejs后,我收到错误:

Error: Could not find matching close tag for "<%=".
   at /Users/jonathan/server/node_modules/ejs/lib/ejs.js:514:19
   at Array.forEach (native)
   at Object.Template.generateSource  (/Users/jonathan/server/node_modules/ejs/lib/ejs.js:501:15)
   at Object.Template.compile (/Users/jonathan/server/node_modules/ejs/lib/ejs.js:423:12)
   at Object.compile (/Users/jonathan/server/node_modules/ejs/lib/ejs.js:288:16)
   at handleCache (/Users/jonathan/server/node_modules/ejs/lib/ejs.js:147:16)
   at View.exports.renderFile [as engine] (/Users/jonathan/server/node_modules/ejs/lib/ejs.js:350:14)
   at View.render (/Users/jonathan/server/node_modules/express/lib/view.js:93:8)
   at EventEmitter.app.render (/Users/jonathan/server/node_modules/express/lib/application.js:566:10)
   at ServerResponse.res.render (/Users/jonathan/server/node_modules/express/lib/response.js:938:7)
   at module.exports (/Users/jonathan/server/app.js:106:11)
   at Layer.handle_error (/Users/jonathan/server/node_modules/express/lib/router/layer.js:58:5)
   at trim_prefix (/Users/jonathan/server/node_modules/express/lib/router/index.js:300:13)
   at /Users/jonathan/server/node_modules/express/lib/router/index.js:270:7
   at Function.proto.process_params (/Users/jonathan/server/node_modules/express/lib/router/index.js:321:12)
   at IncomingMessage.next (/Users/jonathan/server/node_modules/express/lib/router/index.js:261:10)

错误页面来源如上所述。

我能做些什么(理想情况下)解决主要伤害?

谢谢,

1 个答案:

答案 0 :(得分:0)

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

这两行可能相互冲突。删除第一行,删除所有.jade和.html文件,并仅保留.ejs文件。将您的error.html更改为error.ejs。一切都应该按预期工作。