2

I picked up a book on Web Development with MongoDB and Node.js. I've come to a point where my code is not working and I feel like I have everything correct judging by what the book has. Right now I'm getting this error:

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) 

I have an index file in the specified path. Here is my structure for my files:

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

And code to the files that I feel might be wrong.

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'); } }; 

Let me know if there are any other files you would like to see. I'm unsure of how to proceed from here after reviewing the book several times. The only thing I can think of is that there is some disconnect between Express 3.5.X and Express 4.0. The book covered how to convert the server.js, configure.js and the route.js to Express 4.0. But, I'm wondering if there is something I did wrong in other files that could possible not be compliant with 4.0.

2 Answers 2

3

Try doing this in your server.js file

app.set('views', path.join(__dirname, 'views')); 
Sign up to request clarification or add additional context in comments.

1 Comment

That gives me a bunch of errors when trying to start the server. "Failed at the [email protected] start script 'node server.js'.
0

Well that's embarrassing. I was typing "index.handlebars" to save my files. But, they weren't saving them as HANDLEBARS files, they were getting saved as html documents still some how. I changed all the files to the proper file type and all is working now... Not sure why they weren't getting saved properly at the beginning, could have sworn I had it set up correctly XD thanks for everyone that viewed this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.