pillarjs / hbs

Express view engine wrapper for Handlebars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: Failed to lookup view "index" in views directory - Where is problem?

KamilGadawski opened this issue · comments

Hi,
Why can't I find my index.hbs file after installing hbs?

Below is my code:

const path = require('path');
const express = require('express');

const app = express();
const publicPath = path.join(__dirname, '../public');

app.set('view engine', 'hbs');
app.use(express.static(publicPath));

app.get('', (req, res) => {
    res.render('index');
});

app.get('/forecast', (req, res) => {
    res.send({
        location: 'Kielce',
        forecast: 'Jest zimno'
    });
});

app.listen(3000, () => {
    console.log('Serwer uruchomiony - port 3000');
});

Directory structure:

-node_modules
-public
  -css
  -js
    news.html
-src
  app.js
-views
 index.hbs

end error:

Error: Failed to lookup view "index" in views directory "XXX\web-server\src\views"
    at Function.render (XXX\web-server\node_modules\express\lib\application.js:580:17)
    at Function.render (XXX\web-server\node_modules\hbs\lib\hbs.js:277:21)
    at ServerResponse.render (XXX\web-server\node_modules\express\lib\response.js:1012:7)
    at app.get (XXX\web-server\src\app.js:13:9)
    at Layer.handle [as handle_request] (XXX\web-server\node_modules\express\lib\router\layer.js:95:5)
    at next (XXX\web-server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (XXX\web-server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (XXX\web-server\node_modules\express\lib\router\layer.js:95:5)
    at XXX\web-server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (XXX\web-server\node_modules\express\lib\router\index.js:335:12)

You have to actually create the index.hbs file. This module will simply render the files that you create. If you haven't created any, then that would be why you're having that error.

The error is actually coming from Express and not from this module, though. If you need more help, you may want to open the issue at Express, which also has more people who can help (I am away currently).

Also, I see based on the error message, Express is looking in the folder "XXX\web-server\src\views" but the structure you show has the views folder outside of the src folder. You can either change Express to look in the right folder or move your views folder where Express is looking for it at.

bro there is no module problem its your problem i m also facing this problem but now i fix this nd this was very silly mistake made by me and you so solution is : currently you are running your server in XXX\web-server\src\views you have to move views from src to web-server directory and then shift terminal to webserver directory (XXX\web-server>) and then run this command --->node src/FileName.js .i hope your problem would solve. if not then comment me out...

bro there is no module problem its your problem i m also facing this problem but now i fix this nd this was very silly mistake made by me and you so solution is : currently you are running your server in XXX\web-server\src\views you have to move views from src to web-server directory and then shift terminal to webserver directory (XXX\web-server>) and then run this command --->node src/FileName.js .i hope your problem would solve. if not then comment me out...

thanx bro...for this solution