9

Source Code

app.js

var express = require('express');
var app = express();
var path = require('path');
var viewPath = path.join(__dirname, 'app/views');

app.set('views', viewPath);
app.set('view engine', 'jade');

app.get('/', function(req, res) {
    res.render('index', { title: 'Home Page' } );
});

app.listen(3000);

Folder Structure

app
└───views     
│        └───index.jade
└───app.js

Error in Browser

Error: Failed to lookup view index in views directory d:\Users\Admin\Documents\...\project\views

Question

I would like to structure my app by placing the view files in app/views/*.jade, but I cannot get it working so far, using app.set('views', ...) should work but it doesn't

console.log(viewPath) shows d:\Users\Admin\Documents\...\project\app\views

I also tried e.g. app.set('views', 'xxx') but the error still get stucked on the same path, it seems like app.set() has never been called, what's wrong here ?, please guide.

Thanks

Edit

It doesn't matter what I set using app.set('views', 'xxx') the error will always be Error: Failed to lookup view index in views directory d:\Users\Admin\Documents\...\project\views (always keep saying the same path)

I'm so sorry about router.get('/', ...), My actually project's files are different, so I was making mistake here

5 Answers 5

10

Try using

app.set('views', path.join(__dirname, 'views'));

Your app.js is in your app folder so I think

var viewPath = path.join(__dirname, 'app/views');

app.set('views', viewPath);

will look into app/app/views/ instead of app/views/ because of __dirname
__dirname is the directory in which the currently executing script resides.

Sign up to request clarification or add additional context in comments.

2 Comments

My app.js is outside the app folder, thanks anyway
And you typed router.get instead of app.get or you didn't defined router
1

I know it's too late, but might help someone who has the same problem. If you placed the rendering code in a separate file, then you need to use express.Router instead of app.get. For example,

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
  res.render('index', {title: 'Hey', message: 'Hello there!'});
});

module.exports = router;

Then in your app.js,

const router = require('./app/routes/routerName');
app.use('/', router);

Comments

0

I actually have separated files in my project, so I have app.get('/', ...) in the separated route file, then require it to use with app.use()

I really have no ideas but after moving only the app.get('/', ...) to the app.js, the problem has been solved

Thanks everyone

Comments

0

As mentioned by Roxinagi, you should be using app.get(). Everything else seems fine

app.get('/', function(req, res) {
   res.render('index', { title: 'Home Page' } );
});

Comments

0
const path = require('path')
app.set('views', path.join(__dirname, 'app/views/'))
app.set('view engine','jade');

1 Comment

please add some explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.