I'm converting PHP templates over to EJS in Node.js/Express. The template files are somewhat complex, so debugging them is not easy. Node isn't giving me any useful information for debugging, so it's making this task near impossible. Here's a sample error message.
SyntaxError: Unexpected string in "app/views/profile/index.ejs"
at Object.Function (<anonymous>)
at exports.compile (/Users/Gavin/Web/node_modules/ejs/lib/ejs.js:237:14)
at Object.exports.render (/Users/Gavin/Web/node_modules/ejs/lib/ejs.js:284:10)
at View.exports.renderFile [as engine] (/Users/Gavin/Web/node_modules/ejs/lib/ejs.js:318:20)
at View.render (/Users/Gavin/Web/node_modules/express/lib/view.js:76:8)
at Function.app.render (/Users/Gavin/Web/node_modules/express/lib/application.js:502:10)
at ServerResponse.res.render (/Users/Gavin/Web/node_modules/express/lib/response.js:777:7)
at Object.exports.profile_index [as handle] (/Users/Gavin/Web/app/controllers/routes/profile.js:72:6)
at next_layer (/Users/Gavin/Web/node_modules/express/lib/router/route.js:103:13)
at Object.exports.verify_user [as handle] (/Users/Gavin/Web/app/controllers/routes/account.js:305:10)
Finding the source of such a vague error message is extremely tedious. Is there an easier way to debug EJS files?
There are a number of template engines available today, and the more popular ones include Pug (fka Jade), Handlebars, EJS, Mustache, Swig, and others. This post will discuss the following template engines for Express: Pug. EJS.
EJS (Embedded JavaScript Templating) is one of the most popular template engines for JavaScript. As the name suggests, it lets us embed JavaScript code in a template language that is then used to generate HTML.
var http = require('http'); var ejs = require('ejs'); var server = http. createServer(function(req, res){ res. end(ejs. render('Hello World')); }); server.
I agree that Error Messages are vague! There are some open source applications which are making better Error handling in their applications. I use Mean.Js
Their messages are easier to understand and it actually point to a line of code and give you some hint to correct it. Its easy to work with.
I hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With