I've been trying to get handlebars to work with node using. My book has instructed me to install handlebars like this: npm install --save express3-handlebar
. That threw an error
npm WARN deprecated express3-handlebars @0.5.2: THIS PACKAGE HAS BEEN RENAMED TO: express-handlebars
so I tried npm install --save express-handlebar
.
When I tried to start the server node meadowlark.js
, the command prompt displayed Express started on....
But when I put localhost into the browser I received the following:
Error: No default engine was specified and no extension was provided.
at new View (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\view.js:48:42)
at EventEmitter.app.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\application.js:509:12)
at ServerResponse.res.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\response.js:904:7)
at require.create.defaultLayout (C:\Users\myUserName\Desktop\project\meadowlark\site\meadowlark.js:20:6)
at Layer.handle_error (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\layer.js:58:5)
at trim_prefix (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:269:13)
at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:238:9
at Function.proto.process_params (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:313:12)
at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:229:12
at Function.match_layer (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:296:3)
Then I noticed in my index file I still had
var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
So I changed that to remove the 3
from the third line. Still the same long lines of errors. What have I done wrong to receive this error? Am I not in the correct directory when I do the handlebars install? I have tried it from my app root directory, and the file within it that contains my index.js
var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req, res) {
res.render('about');
});
// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});
app.listen(app.get('port'), function() {
console.log('Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.');
});
var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
The example I'm working with is from Web Development with Node & Express (Ethan Brown).
EJS Is Way Faster Than Jade And Handlebars. EJS Has A Really Smart Error Handling Mechanism Built Right Into It. It Points Out To You, The Line Numbers On Which An Error Has Occurred So That You Don't End Up Looking Through The Whole Template File Wasting Your Time In Searching For Bugs.
The main issue I see with your code is that you are creating two instances of app
:
var app = express(); // I see this line twice in your code
This means you are configuring your handlebars on the wrong instance of your app, hence "No default engine was specified"
.
If you remove the second instantiation you should be golden. Here is an example of how I would rewrite that code:
var express = require('express');
var app = express();
var handlebars = require('express-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3000);
app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req, res) {
res.render('about');
});
// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});
app.listen(app.get('port'), function() {
console.log('Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.');
});
You can try that with and without the 3
in your require statement, but it should work either way.
Hope this 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