Using Twig render engine with Express

1.8k Views Asked by At

I've decided to make the jump from building my websites with slim, php and twig to using node.js and express.

To limit the shock of changing my whole process I want to keep using slim as the template engine instead of the default jade.

I have tried to change the code to change the render engine over to twig but now I am getting the following error message. Does anyone know what I am doing wrong? I am at a loss as to why it is not working. Any help would be very helpful.

Error message:

Error: Failed to lookup view "error" in views directory "/var/www/html/SocialTrackers/app/views" at Function.render (/var/www/html/SocialTrackers/app/node_modules/express/lib/application.js:580:17) at ServerResponse.render (/var/www/html/SocialTrackers/app/node_modules/express/lib/response.js:1008:7) at /var/www/html/SocialTrackers/app/app.js:38:7 at Layer.handle_error (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/layer.js:71:5) at trim_prefix (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:315:13) at /var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:335:12) at next (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:275:10) at Layer.handle_error (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/layer.js:67:12) at trim_prefix (/var/www/html/SocialTrackers/app/node_modules/express/lib/router/index.js:315:13)

This is the code I am currently running. Other than changes to shift over the using twig as the render engine, I have made no other changes to the initial express install.

app.js

var createError = require('http-errors');
var twig = require("twig");
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

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

var app = express();

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

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/'))

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
 next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
 // set locals, only providing error in development
 res.locals.message = err.message;
 res.locals.error = req.app.get('env') === 'development' ? err : {};

 // render the error page
 res.status(err.status || 500);
 res.render('error');
});

module.exports = app;

Additionally, I am the following file structure (default express)

app
--bin 
--node_modules
--public
--routes
----index.js
----users.js
--views
----error.twig
----index.twig
----landingpage.twig
--app.js
1

There are 1 best solutions below

0
On BEST ANSWER

Took me a few days to figure this one out.

As I said I'm new to Nodejs and express so I believe I made a very rookie mistake. I thought I would post the solution incase anyone else has this issue.

I was running the node server on a ec2 instance. While I was updating the code it turns out I had to restart the node server as well.

I'm using pm2 to keep the server running all the time so for me the solution was to run the following command.

pm2 restart [namespace of server] 

Note: for me, I never gave the server a namespace so it was simply 'default'. If you don't know the namespace you can find it by running 'pm2 list'

pm2 restart default