Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Failed to lookup view" in basic Express.js application

I'm trying to learn Express.js, so I created simple Express app. The app.js looks as follows:

var express = require('express'); 
var app = express();

app.configure(function(){
    app.set('view engine', 'jade'); 
    app.set('views', __dirname);
});

app.get('/', function(req, res){ 
    res.render("index");
});

app.listen(8080);

My folders structures looks following: enter image description here

However, when I point my browser to http://localhost:8080 I'm getting the following output:

Error: Failed to lookup view "index"
at Function.app.render (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/application.js:492:17)
at ServerResponse.res.render (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/response.js:721:7)
at /Users/kuba/Documents/ws_javascript/express_spike/app.js:10:6
at callbacks (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:161:37)
at param (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:33:10)
at next (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/node_modules/connect/lib/proto.js:199:15)
at Object.expressInit [as handle] (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/middleware.js:31:5)

I've checked and the permissions for the files are ok. Any ideas why express cannot find my index.jade file?

like image 476
Jakub Avatar asked Mar 21 '23 11:03

Jakub


1 Answers

Try this code:

var express = require('express');
var app = express();

app.use(express.logger('dev'))

app.set('views', __dirname+'/views');
app.set('view engine', 'jade');

app.get('/', function(req, res){
    res.render("index");
});

app.listen(8080);

Then place your index.jade inside a folder called views.

And make sure you have jade inside your node_modules folder

like image 200
Pedro Nasser Avatar answered Mar 23 '23 02:03

Pedro Nasser