500错误:无法查找视图"索引"在views目录

时间:2015-05-12 05:10:06

标签: javascript node.js express

我选择了一本关于使用MongoDB和Node.js进行Web开发的书。我已经到了我的代码无效的地步,我觉得我的一切都是正确的,根据这本书的内容来判断。现在我收到了这个错误:

Connect
500 Error: Failed to lookup view "index" in views directory "C:\imageUploadProject/views"
   at EventEmitter.app.render (C:\imageUploadProject\node_modules\express\lib\application.js:555:17)
   at ServerResponse.res.render (C:\imageUploadProject\node_modules\express\lib\response.js:938:7)
   at module.exports.index (C:\imageUploadProject\controllers\home.js:5:7)
   at Layer.handle [as handle_request] (C:\imageUploadProject\node_modules\express\lib\router\layer.js:82:5)
   at next (C:\imageUploadProject\node_modules\express\lib\router\route.js:110:13)
   at Route.dispatch (C:\imageUploadProject\node_modules\express\lib\router\route.js:91:3)
   at Layer.handle [as handle_request] (C:\imageUploadProject\node_modules\express\lib\router\layer.js:82:5)
   at C:\imageUploadProject\node_modules\express\lib\router\index.js:267:22
   at Function.proto.process_params (C:\imageUploadProject\node_modules\express\lib\router\index.js:321:12)
   at next >(C:\imageUploadProject\node_modules\express\lib\router\index.js:261:10)

我在指定的路径中有一个索引文件。这是我的文件结构:

>-controllers
>>>+home.js

>>>+image.js

>-helpers

>-node_modules

>>>Needed modules and such. Won't bore you with all the ones installed. Let me know if there is something in here you would like to see.

>-public

>-server

>>>+configure.js

>>>+routes.js

>-views

>>>-layouts

>>>>>>+main.handlebars

>>>-partials

>>>>>>+comments.handlebars

>>>>>>+popular.handlebars

>>>>>>+stats.handlebars

>>>+image.handlebars

>>>+index.handlebars

>package.json

>server.js

对我认为可能错误的文件进行编码。

server.js

// JavaScript Document
var express = require('express'),
config = require('./server/configure'),
app = express();

app.set('port', process.env.PORT || 3300);
app.set('views', __dirname + '/views');
app = config(app);

var server = app.listen(app.get('port'), function() {
    console.log('Server up: http://localhost:' + app.get('port'));  
});

configure.js

// JavaScript Document
var path = require('path'),
routes = require('./routes'),
exphbs = require('express3-handlebars'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler');
moment = require('moment');

module.exports = function(app) {

app.engine('handlebars', exphbs.create({
    defaultLayout: 'main',
    layoutsDir: app.get('views') + '/layouts',
    partialsDir: [app.get('views') + '/partials'],
    helpers: {
        timeago: function(timestamp) {
            return moment(timestamp).startOf('minute').fromNow();
        }
    }
}).engine);

app.set('view engine', 'handlebars');

app.use(morgan('dev'));

app.use(bodyParser({
    uploadDir:path.join(__dirname, '.../public/upload/temp')
}));

app.use(methodOverride());

app.use(cookieParser('some-secret-value-here'));

routes.initialize(app, new express.Router());

app.use('/public/', express.static(path.join(__dirname, '.../public')));

if('development' === app.get('env')) {  
    app.use(errorHandler());
}

return app;
};

routes.js

// JavaScript Document

var home = require('../controllers/home'),
image = require('../controllers/image');

module.exports.initialize = function(app, router) {
router.get('/', home.index);
router.get('/images/:image_id', image.index);

router.post('/images', image.create);
router.post('/images/:image_id/like', image.like);
router.post('/images/:image_id/comment', image.comment);

app.use('/', router);   
}

home.js

// JavaScript Document

module.exports = {
index: function(req, res) {
    res.render('index');    
}
};

image.js

// JavaScript Document

module.exports = {
index: function(req, res) {
    res.render('image');    
},
create: function(req, res) {    
    res.send('The image:create POST controller');
},
like: function(req, res) {
    res.send('The image:like POST controller');
},
comment: function(req, res) {
    res.send('The image:comment POST controller');  
}
};

如果您还有其他想要查看的文件,请与我们联系。在多次审阅本书后,我不确定如何从这里开始。我唯一能想到的是Express 3.5.X和Express 4.0之间存在一些脱节。本书介绍了如何将server.js,configure.js和route.js转换为Express 4.0。但是,我想知道在其他可能不符合4.0的文件中是否有错误。

2 个答案:

答案 0 :(得分:3)

尝试在server.js文件中执行此操作

#!/usr/bin/env bash

echo "Main Menu"

# Define the choices to present to the user, which will be
# presented line by line, prefixed by a sequential number
# (E.g., '1) copy', ...)
choices=( 'copy' 'exit' )

# Present the choices.
# The user chooses by entering the *number* before the desired choice.
select choice in "${choices[@]}"; do

  # If an invalid number was chosen, $choice will be empty.
  # Report an error and prompt again.
  [[ -n $choice ]] || { echo "Invalid choice." >&2; continue; }

  # Examine the choice.
  # Note that it is the choice string itself, not its number
  # that is reported in $choice.
  case $choice in
    copy)
      echo "Copying..."
      # Set flag here, or call function, ...
      ;;
    exit)
      echo "Exiting. "
      exit 0
  esac

  # Getting here means that a valid choice was made,
  # so break out of the select statement and continue below,
  # if desired.
  # Note that without an explicit break (or exit) statement, 
  # bash will continue to prompt.
  break

done

答案 1 :(得分:0)

那太尴尬了。我输入“index.handlebars”来保存我的文件。但是,他们没有将它们保存为HANDLEBARS文件,它们被保存为html文档仍然有些如何。我将所有文件更改为正确的文件类型,现在一切正常...不知道为什么他们在开始时没有得到正确的保存,可能已经发誓我已经正确设置了XD感谢所有观看此内容的人。< / p>