Error: Failed to lookup view in Express

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:38:58

npm install express@2.5.9 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'));

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.

Ahsan Ullah Sarbaz

I had the same error at first and i was really annoyed. you just need to have ./ before the path to the template

res.render('./index/index');

Hope it works, worked for me.

It is solved by adding the following code in app.js file

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);

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

Check if you have used a proper view engine. In my case I updated the npm and end up in changing the engine to 'hjs'(I was trying to uninstall jade to use pug). So changing it to jade from hjs in app.js file worked for me.

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

use this code to solve the issue

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

Just noticed that I had named my file ' index.html' instead for 'index.html' with a leading space. That was why it could not find it.

This problem is basically seen because of case sensitive file name. for example if you save file as index.jadge than its mane on route it should be "index" not "Index" in windows this is okay but in linux like server this will create issue.

1) if file name is index.jadge

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

2) if file name is Index.jadge

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

I change the views folder name to views_render and also facing the same issue as above, so restart server.js and it works for me.

This error really just has to do with the file Path,thats all you have to check,for me my parent folder was "Layouts" but my actual file was layout.html,my path had layouts on both,once i corrected that error was gone.

I had the same issue and could fix it with the solution from dougwilson: from Apr 5, 2017, Github.

  1. I changed the filename from index.js to index.pug
  2. Then used in the '/' route: res.render('index.pug') - instead of res.render('index')
  3. Set environment variable: DEBUG=express:view Now it works like a charm.

You could set the path to a constant like this and set it using express.

const viewsPath = path.join(__dirname, '../views') 
app.set('view engine','hbs')

 app.set('views', viewsPath)

 app.get('/', function(req, res){

 res.render("index");

});

This worked for me

I had this issue as well on Linux

I had the following

  res.render('./views/index')

I changed it too
  res.render('../views/index')

Everything is now working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!