简体   繁体   English

错误:无法在 Express 中查找视图

[英]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.我正在尝试更好地体验 nodeJS,但我真的不喜欢将所有脚本放在一个文件中。

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应用程序.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环境.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路由.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布局.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索引/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)错误:无法在 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) 在 ServerResponse.render (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\response.js:638:5) 在c:\xampp\htdocs\nodejs\buses\config\routes.js:4:7 回调(c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:177:11)在参数 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:151:11) 在传递 (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\ router\index.js:158:5) 在 Router._dispatch (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:185:4) 在 Object.router [作为句柄] (c:\xampp\htdocs\nodejs\buses\node_modules\express\lib\router\index.js:45:10) 在接下来 (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答案:我找到的唯一解决方案是更改我定义 app.set('views') 和视图引擎的位置

I moved it to the app.js and now is working well.我将其移至 app.js,现在运行良好。

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.我真的不明白这背后的逻辑,但我想它有一个。

Adding to @mihai's answer:添加到@mihai的答案:

If you are in Windows, then just concatenating __dirname' + '../public' will result in wrong directory name (For example: c:\dev\app\module../public ).如果您在 Windows 中,那么仅连接__dirname' + '../public'将导致错误的目录名称(例如: c:\dev\app\module../public )。

Instead use path , which will work irrespective of the OS:而是使用path ,这将与操作系统无关:

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. path.join将规范化路径分隔符并返回正确的路径值。

npm install express@2.5.9 installs the previous version, if it helps. npm install express@2.5.9安装以前的版本,如果有帮助的话。

I know in 3.x the view layout mechanic was removed, but this might not be your problem.我知道在 3.x 中删除了视图布局机制,但这可能不是您的问题。 Also replace express.createServer() with express() express.createServer()替换为express()

Update:更新:

It's your __dirname from environment.js这是你来自 environment.js 的 __dirname
It should be:它应该是:

app.use(express.static(__dirname + '../public'));

It is solved by adding the following code in app.js file通过在 app.js 文件中添加以下代码来解决

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

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

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.希望它有效,对我有用。

You could set the path to a constant like this and set it using express.您可以将路径设置为像这样的常量并使用 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这对我有用

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).就我而言,我更新了 npm 并最终将引擎更改为“hjs”(我试图卸载jade以使用哈巴狗)。 So changing it to jade from hjs in app.js file worked for me.所以将它从 app.js 文件中的 hjs 更改为玉对我有用。

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

In my case, I solved it with the following:就我而言,我用以下方法解决了它:

app.set('views', `${__dirname}/views`);
app.use(express.static(`${__dirname}/public`));

I needed to start node app.min.js from /dist folder.我需要从/dist文件夹启动node app.min.js

My folder structure was:我的文件夹结构是:

从 /dist 启动应用程序

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.例如,如果您将文件保存为 index.jadge,而不是路径上的鬃毛,则在 Windows 中它应该是“索引”而不是“索引”,这没关系,但在像服务器这样的 linux 中,这会产生问题。

1) if file name is index.jadge 1)如果文件名是index.jadge

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

2) if file name is Index.jadge 2)如果文件名是Index.jadge

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

use this code to solve the issue使用此代码解决问题

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

Just noticed that I had named my file ' index.html' instead for 'index.html' with a leading space.刚刚注意到我将文件命名为' index.html'而不是'index.html' ,并带有一个前导空格。 That was why it could not find it.这就是它找不到它的原因。

这个错误实际上只是与文件路径有关,这就是你需要检查的所有内容,对我来说,我的父文件夹是“布局”,但我的实际文件是layout.html ,我的路径在两者上都有布局,一旦我纠正了那个错误是走了。

我将视图文件夹名称更改为 views_render 并且也面临与上述相同的问题,因此重新启动 server.js 并且它对我有用。

I had the same issue and could fix it with the solution from dougwilson: from Apr 5, 2017, Github .我遇到了同样的问题,可以使用 dougwilson 的解决方案来解决它:从 2017 年 4 月 5 日起, Github

  1. I changed the filename from index.js to index.pug我将文件名从index.js更改为index.pug
  2. Then used in the '/' route: res.render('index.pug') - instead of res.render('index')然后在'/'路由中使用: res.render('index.pug') - 而不是res.render('index')
  3. Set environment variable: DEBUG=express:view Now it works like a charm.设置环境变量: DEBUG=express:view现在它就像一个魅力。

I had this issue as well on Linux我在 Linux 上也遇到过这个问题

I had the following

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

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

Everything is now working.现在一切正常。

I had the same issue.我遇到过同样的问题。 Then just check the file directory in your explorer.然后只需检查资源管理器中的文件目录。 Sometimes views folder isn't present.有时视图文件夹不存在。

In my case, I was deploying my web app on a Windows Server and I had a service set up to run a .bat file with only one line as content:就我而言,我在 Windows Server 上部署了我的 Web 应用程序,并且我设置了一个服务来运行一个只有一行作为内容的 .bat 文件:

node D:\webapp\app.js

But this was not enough.但这还不够。 I also had to change the directory before that, so I added the following line at the beginning of the .bat file:在此之前我还必须更改目录,所以我在 .bat 文件的开头添加了以下行:

cd D:\webapp

i had the same problem but, i change the name of the file from index.html to index.ejs and works!!!!我遇到了同样的问题,但是,我将文件名从 index.html 更改为 index.ejs 并且可以工作!!!!

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.render('index');
});

router.get('/contact', (req, res) => {
  res.render('contact', { title: 'Contact Page' });
});

module.exports = router;

and index.js和 index.js

const express = require('express');
const morgan = require('morgan');
const path = require('path');
const app = express();



//settings
app.set('port', 4000);
app.set('views', path.join(__dirname,'views'));
app.set('view engine', 'ejs');

//middlewares

//routes
app.use(require('./routes'));

//static files

//listening
app.listen(app.get('port'), () => {
  console.log('Server is running at http://localhost:'+app.get('port')+'/');
});

update: add this in index:更新:将其添加到索引中:

app.engine('html', require('ejs').renderFile);

app.get("/", (req, res) => {
  res.render("home");
});

// the code below brought the error

app.get("/", (req, res) => {
  res.render("/");
})

I was facing this error because i mistakenly deleted my error.ejs file and it was being called in app.js file and was not found in views as it was already deleted by me我遇到这个错误是因为我错误地删除了我的 error.ejs 文件并且它在 app.js 文件中被调用并且在视图中找不到因为它已经被我删除了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM