I am trying to develop a web page with nodejs express and mongodb. I have followed and analyzed the code from this GitHub page and I'm getting an error while trying to open the web page.
When uncommenting the below code part in app.js and commenting the rest portion of the code in the app.js file (uncommented code part):
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 hbs = require('express-handlebars');
var routes = require('./routes/index');
var app = express();
// view engine setup
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
The following output is obtained:
Cannot GET /
When uncommenting all the statements in the app.js and executing to open the web page, the following output is obtained:
Error: Failed to lookup view "error" in views directory "C:\Users\web\first-1\views"
at EventEmitter.render (C:\Users\web\first-1\node_modules\express\lib\application.js:580:17)
at ServerResponse.render (C:\Users\web\first-1\node_modules\express\lib\response.js:966:7)
at C:\Users\web\first-1\app.js:53:7
at Layer.handle_error (C:\Users\web\first-1\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (C:\Users\web\first-1\node_modules\express\lib\router\index.js:315:13)
at C:\Users\web\first-1\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\web\first-1\node_modules\express\lib\router\index.js:335:12)
at IncomingMessage.next (C:\Users\web\first-1\node_modules\express\lib\router\index.js:275:10)
at done (C:\Users\web\first-1\node_modules\express\lib\response.js:961:25)
at EventEmitter.render (C:\Users\web\first-1\node_modules\express\lib\application.js:582:14)
Kindly help me out of this.
app.set('views', path.join(__dirname, 'views'));
You have specified views directory for your app views. res.render looks for the files in the views directory, not in the sub directories of views. Copy your error.hbs from views/layouts to views/ and try.