Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Failed to lookup view in Express

Note: my auto answer at end of the post

I'm trying to make a better experience of nodeJS and i don't really like to get all the script in one file.

so, following a post here i use this structure

./  config/    enviroment.js    routes.js  public/    css/      styles.css    images  views    index      index.jade    section      index.jade    layout.jade  app.js 

My files are right now:

app.js

var express = require('express'); var app = module.exports = express.createServer();  require('./config/enviroment.js')(app, express); require('./config/routes.js')(app);  app.listen(3000); 

enviroment.js

module.exports = function(app, express) {     app.configure(function() {         app.use(express.logger());         app.use(express.static(__dirname + '/public'));         app.set('views', __dirname + '/views');         app.set('view engine', 'jade'); //extension of views      });      //development configuration     app.configure('development', function() {         app.use(express.errorHandler({             dumpExceptions: true,             showStack: true         }));     });      //production configuration     app.configure('production', function() {         app.use(express.errorHandler());     });  }; 

routes.js

module.exports = function(app) {      app.get(['/','/index', '/inicio'], function(req, res) {         res.render('index/index');     });      app.get('/test', function(req, res) {         //res.render('index/index');     });  }; 

layout.jade

!!! 5 html     head         link(rel='stylesheet', href='/css/style.css')         title Express + Jade     body         #main             h1 Content goes here             #container!= body 

index/index.jade

h1 algoa 

The error i get is:

Error: Failed to lookup view "index/index" at Function.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\application.js:495:17) at render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:614:9) at ServerResponse.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:638:5) at c:\xampp\htdocs\nodejs\buses\config\routes.js:4:7 at callbacks (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:177:11) at param (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:151:11) at pass (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:158:5) at Router._dispatch (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:185:4) at Object.router [as handle] (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:45:10) at next (c:\xampp\htdocs\nodejs\buses\node_modules\express\node_modules\connect\lib\proto.js:191:15)

But i don't really know what is the problem...

I'm starting thinking is because the modules exports...

Answer: Far away the unique solution i found is to change the place i defined app.set('views') and views engine

I moved it to the app.js and now is working well.

var express = require('express'); var app = module.exports = express.createServer();   require('./config/enviroment.js')(app, express);  app.set('views', __dirname + '/views'); app.set('view engine', 'jade');  require('./config/routes.js')(app);  app.listen(3000); 

I don't really understand the logic behind this but i gonna supose it have one.

like image 487
nax Avatar asked Apr 18 '12 19:04

nax


People also ask

Can't set headers after they are sent?

The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written.

Can not find module HTML?

To solve the error "Cannot find module 'html-webpack-plugin'", make sure to install the html-webpack-plugin package by opening your terminal in your project's root directory and running the following command: npm i -D html-webpack-plugin and restart your IDE and dev server.

Why use Node JS?

Node. js is widely used for the back-end of applications, like using Express. js to build the back-end of classic web applications. Also, it is used for server-side programming and non-blocking, event-driven servers like typical websites and backend API services.


2 Answers

Adding to @mihai's answer:

If you are in Windows, then just concatenating __dirname' + '../public' will result in wrong directory name (For example: c:\dev\app\module../public).

Instead use path, which will work irrespective of the OS:

var path = require ('path'); app.use(express.static(path.join(__dirname + '../public'))); 

path.join will normalize the path separator character and will return correct path value.

like image 101
Veera Avatar answered Sep 30 '22 10:09

Veera


npm install [email protected] installs the previous version, if it helps.

I know in 3.x the view layout mechanic was removed, but this might not be your problem. Also replace express.createServer() with express()

Update:

It's your __dirname from environment.js
It should be:

app.use(express.static(__dirname + '../public')); 
like image 45
mihai Avatar answered Sep 30 '22 08:09

mihai