首页 文章

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

提问于
浏览
49

Note :我在帖子末尾的自动回答

我正在尝试更好地体验nodeJS,我真的不想将所有脚本放在一个文件中 .

所以,按照这里的帖子我使用这个结构

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

我的文件现在是:

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

我得到的错误是:

错误:无法在渲染(c:\ xampp \)下的Function.render(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ application.js:495:17)中查找视图“index / index” ServerDesponse.render上的htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ response.js:614:9)(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ response.js:638:5)在c:\ xampp \ htdocs \ nodejs \ _bus \ config \ routes.js:4:7回调(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ router \ index.js:177:11 )在param(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ router \ index.js:151:11)中传递(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ router \ index.js:158:5)在Object.router处的Router._dispatch(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ router \ index.js:185:4)[作为句柄](c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ lib \ router \ index.js:45:10)at next(c:\ xampp \ htdocs \ nodejs \ _bus \ node_modules \ express \ node_modules \ connect \ lib中\ proto.js:191:15)

但我真的不知道问题是什么......

我开始想的是因为模块导出...

Answer: 我找到的独特解决方案是改变我定义app.set('views')和观看引擎的地方

我把它移到了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);

我真的不明白这背后的逻辑,但我会说它有一个 .

10 回答

  • 27

    这个错误实际上只与文件Path有关,那就是你要检查的一切,对我来说我的父文件夹是"Layouts"但是我的实际文件是 layout.html ,我的路径都有布局,一旦我纠正错误消失了 .

  • 0

    添加到@mihai的答案:

    如果您在Windows中,则只需连接 __dirname' + '../public' 将导致错误的目录名称(例如: c:\dev\app\module../public ) .

    而是使用 path ,无论操作系统如何,都可以使用 .

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

    path.join将规范化路径分隔符,并返回正确的路径值 .

  • 32

    起初我有同样的错误,我真的很生气 . 你只需要在模板的路径之前有 ./

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

    希望它有效,为我工作 .

  • 0

    它通过在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");
    });
    
  • 2

    检查您是否使用了正确的视图引擎 . 在我的情况下,我更新了npm并最终将引擎更改为'hjs'(我试图卸载jade以使用pug) . 因此,将它从app.js文件中的hjs更改为jade对我有用 .

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

    使用此代码来解决问题

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

    刚注意到我用 'index.html' 命名了我的文件 ' index.html' ,带有前导空格 . 这就是为什么它找不到它 .

  • 4

    npm install express@2.5.9 安装以前的版本,如果有帮助的话 .

    我知道在3.x中删除了视图布局机制,但这可能不是你的问题 . 也用 express() 替换 express.createServer()

    更新:

    这是来自environment.js的__dirname
    它应该是:

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

    我将views文件夹名称更改为views_render并且还面临与上面相同的问题,因此重新启动server.js并且它适用于我 .

  • 0

    由于区分大小写的文件名,基本上可以看到此问题 . 例如,如果你将文件保存为index.jadge而不是路由中的mane,那么它应该是“索引”而不是Windows中的“索引”这没关系,但是在linux这样的服务器中这会产生问题 .

    1) 如果文件名是 index.jadge

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

    2) 如果文件名是 Index.jadge

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

相关问题