0

I'm trying to display my nodejs app via localhost:3000.

My main JS file is "app.js". I ran in the terminal this: "node app" and the response was "server run", but when I type "localhost:3000" in my browser, the terminal shows this error message: "Error: Failed to lookup view "home" in views directory "C:...\views". Then I ran in the terminal "npm start", and got the same response of "server run", and then the same localhost:3000 browser error: "Error: Failed to lookup view "home" in views directory "C:...\views".

I installed those packages: (1) npm init y-, THEN package.json, (2) express-handlebars (and added views\main.handlebar + views\home.handlebars files).

My code in the file app.js is the following:

const express = require('express');
const exphbs = require('express-handlebars');
const app = express();

app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');

app.use(express.static('public'));

app.get('/', (req, res) => {
  res.render('home', { title: 'Home Page' });
});

app.get('/about', (req, res) => {
  res.render('about', { title: 'About Us Page' });
});

app.listen(3000, () => console.log('server run!'));

This is the printscreen that maybe will help more

Looking up in questions here, here and here didn't help me to solve my problem. What should L do to fix in and make localhost:3000 run properly in my browser?

Yair Shachar
  • 55
  • 1
  • 11

1 Answers1

1

You have your home.handlebars file in the wrong folder.

As you can see from the error message, it is expected to be found as views/home.handlebars, however you have it in views/layouts/home.handlebars!

The views/layouts folder should contain only layouts (such as your main.handlebars which is apparently configured as layout).

(I guess the about.handlebars file should go in views as well...)

enter image description here

It should look like this instead:

enter image description here

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • Not sure. look at row 5 of app.js, It says: app.engine('handlebars', exphbs({ defaultLayout: 'main' }));.... The full address is: Error: Failed to lookup view "home" in views directory "C:\nodeJS\nodejs book\three\views".... If I move the home and\or the main file outside of the layout folder to the views folder, I get in my browser this error: Error: ENOENT: no such file or directory, open 'C:\nodeJS\nodejs book\three\views\layouts\main.handlebars' – Yair Shachar Mar 15 '20 at 10:25
  • When I left just "main.handlebars" in views\layouts folder, and moved the home & about files out to views folder, it works fine. is this the right solution? – Yair Shachar Mar 15 '20 at 10:42
  • Yes this is what I showed above. `main` is a layout (you set it as `defaultLayout`), so it goes into `views/layouts`. `home` and `about` are regular views, they go in `views`. – CherryDT Mar 15 '20 at 11:29
  • If this solution works for you, you can mark the answer as accepted, marking the question as solved. Thank you! – CherryDT Mar 15 '20 at 19:12