Express Error - 500 Error: Failed to lookup view "index" - node.js

When using Express version 4.15.2, the following (apparently commonly encountered) error appeared in the browser--- 500 Error: Failed to lookup view "index" at Function.app.render. I've read the solutions offered to several such victims, but my __dirname value is indeed pointing to the correct views folder, and the correct path for 'views' is indeed constructed in the app.js file which I'm using in the Windows command line (i.e., >node app.js). The only thing I notice that seems strange is that the 'index' file present in the views folder is named 'index.jade'. I'm using pug instead of jade. In fact, hasn't use of Jade been discontinued? Could this be the problem? Should there be a '.pug' extension now for windows, or no extension at all on the index file?
Per your request to see the configuration (although, it's pretty standard, and I didn't change anything but the 'jade', to 'pug'), the following is the only configuration I see in app.js. (And changing the view engine from 'jade' to 'pug' got rid of the error of not finding 'jade'.)
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' === app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);

Yes, indeed, all I had to do was to change the extension .jade to the extension .pug on both the index and the layout file, and that solved the problem. Thus, the problem wasn't __dirname; that was indeed correct. The problem was that it didn't like the .jade extension.
Thanks for the ideas anwyay.

Related

Express NodeJS Cannot find module 'html'

I have a Node Server with Express. I get the cannot find module 'html' error even though my code looks like this and should be correct in my opinion:
app.set('views', path.join(__dirname, 'build/views'));
app.use(favicon(path.join(__dirname, "build/favicon.ico")));
app.use('/scripts', express.static(path.join(__dirname, 'node_modules')));
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function(req, res){
res.render('index.html');
});
You have to set engine for HTML
Include this code in your main file
var engines = require('consolidate');
app.engine('html', engines.mustache);
app.set('view engine', 'html');
If you only want to serve a static file without passing any variables from the server to the client the easiest solution would be:
res.sendFile(path.join(__dirname + '/build/views/index.html'));
Especially when you are using AngularJS for the client side. The problem with the solution above is that mustache uses {{}} as variables recognition. So does AngularJS which might causes errors!
It seems that you are trying to display a static index.html file but you serve it as if it was a template. Probably Express is trying to find a module for html template format, which doesn't exist.
You may try to send the index as a static file instead of with res.render which is for rendering templates.
Some time ago I wrote an example of serving static files with Express. It's available on GitHub:
https://github.com/rsp/node-express-static-example
See also my other answer, where I explain it in more detail.

Nodejitsu - Error: No default engine was specified and no extension was provided

I'm having a problem with Nodejitsu. My app runs perfectly when deployed locally but I've recently been introduced to nodejitsu and I've uploaded my app, database and everything.
However, when I try to deploy my application, I keep getting "Internal Server Error".
When I view the log, it tells me:
Error: No default engine was specified and no extension was provided.
I specified both the viewing engine in the package.json and in the app.js file that is charged with controlling the application.
app.configure('development', function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use(express.logger('dev'));
app.use(express.bodyParser({uploadDir: './uploads' }));
app.use(express.cookieParser());
app.use(express.session({secret: 'mySecret', cookie: {maxAge: 25920000000}}));
app.use(express.errorHandler());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
The package.json is
"engines": {
"node": "0.10.x",
"jade": "0.31.0"
}
As you can see, I'm clearly setting up the view engine but I don't understand why the am getting that error.
I've also scanned the lib/views.js file for the line that threw the error
if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');
Which makes sense when you think of why the error was thrown; however, becomes confusing when I know for certain that I specified a viewing engine.
Ah, omit the app.configure wrapper entirely. Just take the code inside it and have it be top-level. Nodejitsu is running your app with NODE_ENV=production, not development. In general, any configuration that should always be run, such as all of your code above should NOT be put in an app.configure callback. I never use app.configure at all as it is so trivial to be basically useless, but it's purpose is for things that differ between development and production environments.

Opening Page with Express

I have read around about this a lot and it seems like its an issue that confuses a lot people. I am doing a project with express and I dont want to you any tempting engines, I am using backbone with underscore and thats enough for me. I want to write plain HTML and render them..
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set("view options", {layout: false});
app.use(express.static(__dirname + '/views'));
app.set('views', __dirname + '/views');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use( express.cookieParser() );
app.use(express.session({ secret: 'topsecret' } ));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
this is what I have so far.. I am having a hard time to understand how express knows to render the index.html file as my home page even though I have this:
app.get('/', function(req, res){
console.log("Heyyyyyy");
});
I would expect nothing to be rendered and "Heyyyy" to be printed but express renders the index.html and doesnt print "Heyyyy"
Express evaluates middleware in the order in which they are configured.
You have a static file handler before your app.router.
Take the index.html file out of the __dirname + '/views' directory.
You also have a static file handler at the bottom of your middleware stack. Static file handlers should be at the top of your middleware stack. When they're at the bottom of your middleware stack each request unnecessarily processes all of the stack. For example, you don't need to run express.bodyParser() when serving static files.

With EJS templates deep urls wont resolve the public resources properly

Im using ejs as template engine with default layout off, but i activate it on demand via {layout:True}, i have two urls '/' and '/session/new' the css and js are show OK in the former but in the second one they would adress to 'http://localhost/sessions/public/stylesheets/my.css'
i tried using *stylesheet_link_tag* as seen here (http://railwayjs.com/), but i guess its only for railwayjs, is there anything im missing,
im thinking of a workaround of addind a depth variable to precede the link and that would be filled with '/..' depending on the route
here is my configure
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', {layout: false});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

Node.js not parsing external javascripts correctly

So the problem is that node.js doesn't send my javascript files correctly. I can find them in the html code, but they are not executed by the browser. Those js files don't even appear in the firebug's NET panel when I reload the page.
I am using Express with Jade.
Any ideas how to fix this issue? Or what's causing it?
Make sure you have express.static set properly in app.configure.
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
// Also, make sure you have the most updated versions of express and node.js. Both change often.

Resources