-1

i am using handlebars with js rendering through express I am having issues solving the error. every time I run the localserver/300 I get an Error: Failed to lookup view "error" in views directory . not sure if I am missing something please see my code below.

the error 

Error: Failed to lookup view "error" in views directory "C:\Users\Sue\myCommunityAppCh3\views"


        // view engine setup
    var createError = require('http-errors');
    var express = require('express');
    var exphbs = require('express-handlebars');
    var path = require('path');``
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');`

var indexRouter = require('./app_server/routes/index');
var usersRouter = require('./app_server/routes/users');

var app = express();



// view engine setup


app.set('view engine','handlebars');
app.set('views', path.join(__dirname, '/public/views'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));


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

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());


app.use('/', indexRouter);
app.use('/users', usersRouter);

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

    enter code hereenter code here

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
Zrushb
  • 43
  • 1
  • 6
  • 1
    You have neglected to include said code. In order for us to help you better, please update your question so that it shows your existing code and details any failed [**attempts made so far**](http://meta.stackoverflow.com/questions/261592) in a [**minimal, complete, and verifiable example**](http://stackoverflow.com/help/mcve), along with clearly stating what your desired **result** is. For further information, please refer to the help article regarding [**how to ask good questions**](http://stackoverflow.com/help/how-to-ask), and take the [**tour of the site**](http://stackoverflow.com/tour). – Obsidian Age Oct 28 '18 at 22:52
  • please can you see above – Zrushb Oct 29 '18 at 10:02

1 Answers1

0

I don't see much code, but make sure your views folder is inside public and make sure you tell express to use public as a static path (app.use(express.static('public'))).

Ziyo
  • 604
  • 5
  • 10
  • Hi , I have moved the views folder to public . I am still getting the error Error: Failed to lookup view "error" in views directory "C:\Users\Sue\myCommunityAppCh3\views" please look above for pasted coed for app.js – Zrushb Oct 29 '18 at 09:54
  • In Express the order of your code matters. Try putting `app.use(express.static(path.join(__dirname, 'public')));` before you do `app.set` or `app.use`. Also try putting `app.set('views', path.join(__dirname, '/public/views'));` after you register handlebars (`app.set('view engine','handlebars');`). – Ziyo Oct 29 '18 at 13:07
  • i have edited the code and still get the same results – Zrushb Oct 29 '18 at 18:57
  • app.use(express.static(path.join(__dirname, 'public'))); app.engine('handlebars', exphbs({defaultLayout: 'main'})); app.set('view engine','handlebars'); app.set('views', path.join(__dirname, '/public/views')); – Zrushb Oct 29 '18 at 18:57
  • would there be any thing else in the app.js that would cause the error to re-occur . please help – Zrushb Oct 29 '18 at 18:58
  • It's really hard to debug this. Is there any way you can share you code with me? I will run it locally and help you out. – Ziyo Oct 29 '18 at 19:35
  • Hi Ziyo, thank you very much and i appreciate it so much . Would it be possible if i can have your email address so I can zip over the compressed folder over to you? – Zrushb Oct 29 '18 at 23:45
  • Nikifr.91@gmail.com – Ziyo Oct 30 '18 at 02:14
  • Thank you , You would be expecting a email from my email address missabailey@hotmail.co .uk with the subject error handlebars, express and js shortly – Zrushb Oct 30 '18 at 09:53