我正在Node.js上使用Express 4 Application。我在渲染新视图时遇到问题

时间:2015-03-17 21:44:25

标签: node.js express

我使用Express生成器生成express 4应用程序。一旦我这样做,我用ejs替换了玉石模板引擎。我现在正试图在/ players添加新路线。当我尝试导航到myapp / players时,我收到了这个错误:

    Error: Failed to lookup view "error" in views directory "/home/fk/d/e/goose/views"
       at EventEmitter.app.render (/home/fk/d/e/goose/node_modules/express/lib/application.js:555:17)
       at ServerResponse.res.render (/home/fk/d/e/goose/node_modules/express/lib/response.js:938:7)
       at module.exports (/home/fk/d/e/goose/app.js:65:7)
       at Layer.handle_error (/home/fk/d/e/goose/node_modules/express/lib/router/layer.js:58:5)
       at trim_prefix (/home/fk/d/e/goose/node_modules/express/lib/router/index.js:300:13)
       at /home/fk/d/e/goose/node_modules/express/lib/router/index.js:270:7
       at Function.proto.process_params (/home/fk/d/e/goose/node_modules/express/lib/router/index.js:321:12)
       at IncomingMessage.next (/home/fk/d/e/goose/node_modules/express/lib/router/index.js:261:10)
       at fn (/home/fk/d/e/goose/node_modules/express/lib/response.js:933:25)
       at EventEmitter.app.render (/home/fk/d/e/goose/node_modules/express/lib/application.js:557:14) 

我的文件结构是

    |Myapp"goose"
     -bin
     -config
     -node modules
     -public
     -routes
     --index.js
     --users.js
     --players.js
     -views
     --index.ejs
     --users.ejs
     --players.ejs
     --error.ejs
     -app.js
     -package.json

以下是我的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 mongoose = require('mongoose');

    var routes = require('./routes/index');
    var users = require('./routes/users');
    var players = require('./routes/players');

    var app = express();

    var configDB = require('./config/database.js');

    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    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('/', routes);
    app.use('/users', users);
    app.use('/players', players);


    // 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: {}
      });
    });


    module.exports = app;

以下是我的路线/ players.js的代码

    var express = require('express');
    var router = express.Router();

    /* GET players page. */
    router.get('/players', function(req, res, next) {
      res.render('players', { title: 'Players' });
    });

    module.exports = router;

我的索引视图目前仍然正常。感谢您的帮助,如果我能提供更多可以提供帮助的信息,请告诉我。

**编辑1:**我添加了一个error.ejs文件,该文件在文件结构中反映出来。我相信导致上述所有错误消息的原因是我的应用程序试图在导航到/ player时呈现error.ejs。现在,当我尝试导航到/ player时,我被重定向到新的error.ejs视图。

编辑2:此问题已解决。有关详细说明,请查看用户Jonathan Lonowski的答案。谢谢Johnathon。

1 个答案:

答案 0 :(得分:1)

最初的问题是,请求GET /players会产生404 Not Found

这是因为路由被定义为GET /players/players,因为Express结合了.use()和路由器路径:

app.use('/foo', fooRouter);
// GET /foo/bar
fooRouter.get('/bar', ...);

指定"前缀"使用.use(),您可以为" root"定义路由。路由器中的那个前缀:

// GET /players = /players + /
router.get('/', ...);