1

I am trying to implement a simple CRUD node.js application using express. In my server.js file I have written the below:

var createError = require('http-errors');
 var express = require('express');
 var path = require('path');
 var cookieParser = require('cookie-parser');
 var logger = require('morgan');
 var expressValidator = require('express-validator');
 var flash = require('express-flash');
 var session = require('express-session');
 var bodyParser = require('body-parser');
 
 var oracledb = require('oracledb');
 var connection  = require('./lib/db');
 
 var indexRouter = require('./routes/index');
 var usersRouter = require('./routes/users');
 var customersRouter = require('./routes/customers');
 
 var app = express();
 
// view engine setup
console.log('check views path join', path.join(__dirname, 'views'))
console.log('check views path join test 2', path.join(path.resolve(), 'views'))

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

 app.use(logger('dev'));
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: true }));
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));
 
 app.use(session({ 
     secret: '123456cat',
     resave: false,
     saveUninitialized: true,
     cookie: { maxAge: 60000 }
 }))
 
 app.use(flash());
 app.use(expressValidator());
 
 app.use('/', indexRouter);
 app.use('/users', usersRouter);
 app.use('/customers', customersRouter);
 
 // catch 404 and forward to error handler
 app.use(function(req, res, next) {
   next(createError(404));
 });
 
 // error handler
 app.use(function(err, req, res, next) {
   // set locals, only providing error in development
   res.locals.message = err.message;
   res.locals.error = req.app.get('env') === 'development' ? err : {};
 // render the error page
   res.status(err.status || 500);
   res.render('error');
 });


// port must be set to 3000 because incoming http requests are routed from port 80 to port 8080
app.listen(3000, function () {
    console.log('Node app is running on port 3000');
});
 module.exports = app; 

when running though I keep getting the below error

Error: Failed to lookup view "error" in views directory "C:\Users\User....\myapp\views.... GET /customers 500"

I have tried solutions from similar posts Express app: Error: Failed to lookup view "index" in views directory "views"

without success though. My code skeleton looks like enter image description here

and I have created it using

npx express-generator
user2829319
  • 239
  • 4
  • 16

1 Answers1

1

when you run express generator it creates the views with the jade engine format. since you are setting the view engine to ejs, the error.jade file will not show up, as it is looking for an error.ejs

In the future if you want express to generate using the ejs engine you can simply pass the -e flag, as per the documentation.

express <your app> -e

The two formats are pretty similar so you might be able to get away with just changing the extension. If not, you will have to recreate the error.jade file in ejs format

about14sheep
  • 1,813
  • 1
  • 11
  • 18
  • I recreated the project using `npx express-generator --view=ejs myapp` so that ejs engine was chosen. This has as a result to add within the view folder the error.ejs – user2829319 Apr 02 '23 at 09:26
  • if this fixed your issue consider marking it as the accepted answer to help others – about14sheep Apr 02 '23 at 13:21