Node.js Express将模板引擎jade更改为ejs

时间:2015-09-19 13:48:38

标签: javascript node.js express

我正在尝试使用以下更改来更改Node.js Express模板引擎。

app.js

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

的package.json

"jade": "~1.11.0","ejs": "^1.0.0",

最后将View/*扩展*.jade更改为*.ejs

但之后我收到了这个错误:

  

无法在视图目录“/ var / www / mynode / myapp / views”中查找视图“index”

Error: Failed to lookup view "index" in views directory "/var/www/mynode/myapp/views"
    at EventEmitter.render (/var/www/mynode/myapp/node_modules/express/lib/application.js:579:17)
    at ServerResponse.render (/var/www/mynode/myapp/node_modules/express/lib/response.js:961:7)
    at /var/www/mynode/myapp/routes/index.js:6:7
    at Layer.handle [as handle_request] (/var/www/mynode/myapp/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/mynode/myapp/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/var/www/mynode/myapp/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/www/mynode/myapp/node_modules/express/lib/router/layer.js:95:5)
    at /var/www/mynode/myapp/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/var/www/mynode/myapp/node_modules/express/lib/router/index.js:330:12)
    at next (/var/www/mynode/myapp/node_modules/express/lib/router/index.js:271:10)

更新

我当前的 app.js 文件:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// Uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// Catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// Error handlers

// Development error handler
// It will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// Production error handler
// No stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;
  

** ls -l查看myapp文件夹**

我的节点:

    drwxr-xr-x  7 rajdeep root    4096 Sep 19 19:16 .
    drwxrwxr-x  3 rajdeep rajdeep 4096 Sep 19 18:32 ..
    -rw-r--r--  1 rajdeep root    1442 Sep 19 19:04 app.js
    -rw-r--r--  1 rajdeep rajdeep 1442 Sep 19 19:04 app.js~
    drwxr-xr-x  2 rajdeep root    4096 Sep 19 17:44 bin
    drwxr-xr-x 10 rajdeep rajdeep 4096 Sep 19 17:44 node_modules
    -rw-r--r--  1 rajdeep root     325 Sep 19 19:16 package.json
    -rw-r--r--  1 rajdeep rajdeep  325 Sep 19 19:16 package.json~
    drwxr-xr-x  5 rajdeep root    4096 Sep 19 17:44 public
    drwxr-xr-x  2 rajdeep root    4096 Sep 19 17:44 routes
    drwxr-xr-x  2 rajdeep root    4096 Sep 19 19:17 views

查看:

-rw-r--r-- 1 rajdeep root     84 Sep 19 18:32 error.ejs
-rw-r--r-- 1 rajdeep root    456 Sep 19 19:16 index.ejs
-rw-r--r-- 1 rajdeep rajdeep 456 Sep 19 18:40 index.ejs~
-rw-r--r-- 1 rajdeep rajdeep 456 Sep 19 19:16 index.ejs~
-rw-r--r-- 1 rajdeep root    125 Sep 19 18:32 layout.ejs

2 个答案:

答案 0 :(得分:1)

在您的app.js文件中,替换

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

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

还您在/ views中创建了3个索引文件吗? 只需保留您需要的一个,然后重命名/替换其他人即可。然后尝试重新运行它。

答案 1 :(得分:0)

您有索引页(根URL)的路由,因此您需要/var/www/mynode/myapp/views文件夹中的index.ejs文件。