Fails to look up view in Express - node.js

This is the structure of my project.
app
-controllers
index.server.controller.js
-models
-routes
index.server.routes.js
-views
index.ejs
config
-express.js
I set the view directory in my express.js file:
app.set('views', '../app/views');
app.set('view engine', 'ejs');
And this is in my controller file:
exports.render = function(req,res) {
res.render('index' , {
title: 'welcome to this page'
})
};
Whenever i open localhost i get
Error: Failed to lookup view "index" in views directory "../app/views/"

You're giving it a wrong path. Try this
app.set('views', __dirname + '/../app/views');

Related

Static files not served by express when using pug

im trying to serve up static files using express and pug as the templating engine but somehow my assets are not being loaded
my files path :
+front
+views
+login
index.pug
+images
+js
+css
...
server.js
here is my server code :
app.use(express.static('front'));
app.set('views', './front/views')
app.set('view engine', 'pug')
router.get('/login', (req, res) => {
res.render('./Login_v1/login', { title: 'Login', message: 'Login'})
})
pug code :
link(rel='icon' type='image/png' href='/images/icons/favicon.ico')
step first add path to your static folder in app.js/server.js(your entry file of express)
app.use(express.static(__dirname + 'images/icons')); // path of your image folder
than you can access it by
href='localhost:serverPort/favicon.ico

After tried all solutions, EJS still failed to lookup view "index.ejs" in views directory

I'm a seasoned programmer learning MEAN stack. Got this err during practice, "
Failed to lookup view 'full path' in views directory 'physical path'.
I thought path is not a complicated thing, surprisingly AFTER search and tried many solutions for the similar err here still not being able to resolve it. Here is my base code of server.js, pay attention to res.render
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
...
app.get('/quotes', (req, res)=>
{ res.sendFile(__dirname + '/index.html'); }) // running ok
// post-to (Create)
app.post('/quotes', (req, res) =>
{
...
{
res.redirect('/');
})
})
app.set('view engine', 'ejs');
app.get('/', (req, res)=>
{
...
{
res.render('index.ejs', ...);
})
})
Here are what I tried and results:
// -- 1) original
res.render('index.ejs', ...);
Failed to lookup view "index.ejs" in views directory "C:\Node.project\views"
// -- 2) set path explicitly ahead of "view engine" using path.join()
var path = require('path');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
res.render('index.ejs', ...);
Failed to lookup view "index.ejs" in views directory "C:\Node.project\mean1\views"
// -- 3) concat, __dirname + '/views'
app.set('views', __dirname + '/views');
res.render('index.ejs', ...);
Failed to lookup view "index.ejs" in views directory "C:\Node.project\mean1/views"
// -- 4) reverse "/" to "\"
app.set('views', __dirname + '\views');
res.render('index.ejs', ...);
Failed to lookup view "index.ejs" in views directory "C:\Node.project\mean1iews"
// -- 5) w/o setting default "views", simply tell where to look for the template
res.render(__dirname + '/MEAN1/views/index.ejs', ...);
Failed to lookup view "C:\Node.project\mean1/views/index.ejs" in views directory "C:\Node.project\views"
My guess is it has something to do with the structure
\Node.project (folder)
|-- package.js, package.json (files)
|
\MEAN1 (folder)
|-- index.html, server.js (files)
|
\views (folder)
|-- index.ejs
How do I start node? Either Nodemon or Node, that is
at Node.project folder,
"nodemon MEAN1/server.js"
Or, at MEAN1 folder,
"node server.js"
.
To my biggest surprise after read and tried many diff ways, simply copied the index.ejs as XX.ejs, in server.js replaced "index" with "XX", Hooray!
Still not sure what's the cause.
I saw a few same err while location and file are all there w/o proper reason. It may suggest Express is the culprit.
Posted here to help others avoid wasting time when having the same unexplainable issue.

How to prevent PM2 to change the current directory?

Im using pm2 to handle my nodejs micro services and express-handlebars to handle the views:
var hbs = exphbs.create({
defaultLayout: 'main',
helpers: {
ifeq: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
toJSON : function(object) {
return JSON.stringify(object);
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
Lunching the app directly (node app.js) works great. But if I lunch it using PM2 (pm2 start app.js) i get:
Error: Failed to lookup view "home" in views directory "/root/views"
When lunching pm2 the current working directory change to /root/ and since my app in not there I got an error from handlebars trying to open the views directory (which is in the app directory).
Is there a way to fix this by telling pm2 the current working directory or by telling the express-handlebars library the complete directory instead of using a relative one?
I am using Koa and SWIG for templates - but I needed to include the path to the views in the app setup:
app.use(koaRender('./server/server-side-views', {
map: { html: 'swig' },
cache: false
}));
I suspect it is more of the same for you. I think in express your code should be something along the line of:
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', __dirname + '/yourViewDirectory');

Failed to lookup view "index" in views directory

I am trying to learn node.js.
I have the following code.
var express = require('express');
var app = express();
var port = process.env.PORT || 5000;
app.use(express.static('public'));
app.use(express.static('src/views'));
app.set('view engine', 'ejs');
app.get('/', function(req, res) {
res.render('index', { title: 'Hello from render', nav: ['Books', 'Author'] });
});
Failed to lookup view "index" in views directory
I have the file named index.ejs. How can I get rid of this error?
Try with
app.set('views', __dirname + '/views');
This is how I fixed it.
app.set('views', './src/views');
app.set('view engine', 'ejs');
I had the same problem too. I have saved the index file as .html file. But it should be index.ejs. then it worked. Check that too
For me it was a banal problem: when I named my 'views' folder, accidentally I typed a white space before the name, so the name was ' veiws', thus, ejs couldn't find the 'views' folder.
i fixed this error by writing below line befor my
app.set('viewengine' , 'ejs')
app.set('views',path.join(__dirname,'views'));
using path is more convenient ,also note to require path befor using it
I had the same issue, then I saved my index file inside the views folder. Now the issue is gone. Also add this line of code in your file
app.set('views', path.join(__dirname, '/views'))

Subfolder views expressjs 3x /nodejs

How can I use a subfolder for my 'views'?
In the main 'app.js' I've set the root folder for the views like so:
app.configure(function(){
app.set('view engine', 'jade');
app.set('views', __dirname + '/apps' );
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/apps' ));
app.use(app.router);
});
And my controller loos like this:
module.exports = function(req, res, base) {
res.render( 'hello/views/');
};
The folder looks like this:
./apps/hello/views/index.js
But still it can't find it. "ERROR: Failed to lookup view "hello/views"
Thanks!
Actually, I made a vastly better solution.
In the app configuration I just set the default view like so:"
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
Later on I inlcude a boot.js (based on the mvc example of expressjs ) in which I include the following:
// allow specifying the view engine
if (obj.engine) app.set('view engine', obj.engine);
app.set('views', __dirname + '/../apps/' + name + '/views');
This will override the view folder to the one specified in the folder 'apps/xxx/views' and will even allow you to specify a new viewtype in the root of that folder like so:
// filename = /apps/hello/index.js
exports.engine = 'jade';
// Render the indexpage
exports.index = function(req, res, next){
res.render('hello');
};
I am surprised that your self answer works, since you should be telling express to render a particular view, and hello/views is a folder. However I'm not familiar with the code pattern you're using for your controller (the base argument is what's throwing me off, since Express middleware uses function(req,res,next).)
My routes look like this:
app.set('views', __dirname + '/apps' );
...
app.get('/hello', function(req,res){
res.render('hello/views/index')
});
In this case ./apps/hello/views/index.jade is a jade file, not a javascript file. If your templates are javascript, perhaps you should set view engine to something other than jade.
You can set views as array of paths
app.set('views', [path.join(__dirname, 'views/users/'), path.join(__dirname, 'views')])
You can check the resulting paths using app.get('views'); which should return an array.
[/server/views/users/, /server/views]
Express will search through the array for available paths. You can then render with just the file name like this
res.render( 'index');
res.render( 'profile');
I fixed this problem by setting the basis view to the root of the directory like so:
app.set('views', __dirname);
And the added the folders from the root on in the controller like so:
res.render( 'apps/hello/views/');

Resources