Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express change the template engine jade to ejs

I am trying to change the Node.js Express template engine by using the following change.

app.js

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

Package.json

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

And finally change the View/* extention *.jade to *.ejs.

But after that I have got this error:

Failed to lookup view "index" in views directory "/var/www/mynode/myapp/views"

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)

UPDATE

My current app.js file:

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 view myapp folder **

My node:

    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

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
like image 953
kasper chexonz Avatar asked Sep 19 '15 13:09

kasper chexonz


1 Answers

In your app.js file, replace

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

with

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

Also, you have created 3 index files in /views? Just keep the one you need to and rename/replace others. And try re-running it.

like image 141
Zircoz Avatar answered Oct 16 '22 23:10

Zircoz