Closed custa1200 closed 8 years ago
I have narrowed this down and it's if I include ad use a route file i.e.:
var express = require('express');
var path = require('path');
var exphbs = require('express-handlebars');
var app = express();
// Routes
var test = require('./routes/test');
var hbs = exphbs.create({
defaultLayout: 'main',
extname: ".hbs",
layoutsDir: path.join(__dirname, "views/layouts/"),
partialsDir: path.join(__dirname, 'views/partials')
});
// Register `hbs.engine` with the Express app.
app.engine('handlebars', hbs.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components', express.static(__dirname + '/../bower_components'));
app.use("/", test);
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3001, function () {
console.log('express-handlebars example server listening on: 3001');
});
module.exports = app;
Even with a simple get in that route file it throws the original error. The route file looks like the following:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('about', {
title: 'About'
});
});
module.exports = router;
Throws the following errors:
Error
at Object.<anonymous> (/Users/custa/github/Portfolio/portfolioapp/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/exception.js:29:23)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/custa/github/Portfolio/portfolioapp/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/base.js:3:17)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
Is it possible that I am running too new a version of Node?
My first thought: perhaps the error is caused by importing the routes before defining the rendering engine? Try moving var router = require('./router')(app);
below app.engine('handlebars', hbs.engine);
and the lines that follow.
[Edit] Could you share your about
view? I've seen similar errors when my Handlebars markup isn't quite right.
{{#contentBlock "head"}}
{{/contentBlock}}
{{>hero/home }}
{{>section/about}}
{{>section/skills}}
{{>section/linkedin}}
{{#contentBlock 'scripts'}}
{{/contentBlock}}
@custa1200 Thanks! This is very much a "turn it off / back on again" approach to your problem, but have you tried removing all the Handlebars logic from that view, just to see if it still errors even without having to parse any template markup? I've had my code break due to a minor syntax error in my Handlebars markup, so sometimes starting from scratch and slowly re-adding code has been helpful for me.
Looks like a had a mismatch which caused the issues with
app.engine('handlebars', hbs.engine);
app.set('view engine', 'hbs');
worked when I made it
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
Hi I am trying to swap out the ExpressJS default hbs router for express-handlebars, yet when I try to do it I get the following error when trying to access a page. What have I done wrong?