Skip to content

Instantly share code, notes, and snippets.

@tejasrsuthar
Last active June 23, 2017 12:28
Show Gist options
  • Save tejasrsuthar/52532d5975ad1344386a2d4778d761d2 to your computer and use it in GitHub Desktop.
Save tejasrsuthar/52532d5975ad1344386a2d4778d761d2 to your computer and use it in GitHub Desktop.
NodeJS server with handlebars usage
// Credit: http://code.runnable.com/U07z_Y_j9rZk1tTx/handlebars-template-examples-with-express-4-for-node-js
var express = require('express');
var app = express();
var expressHbs = require('express3-handlebars');
app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs'}));
app.set('view engine', 'hbs');
app.get('/', function(req, res){
res.render('index');
});
app.get('/simple', function(req, res){
var data = {name: 'Gorilla'};
res.render('simple', data);
});
app.get('/complex', function(req, res){
var data = {
name: 'Gorilla',
address: {
streetName: 'Broadway',
streetNumber: '721',
floor: 4,
addressType: {
typeName: 'residential'
}
}
};
res.render('complex', data);
});
app.get('/loop', function(req, res){
var basketballPlayers = [
{name: 'Lebron James', team: 'the Heat'},
{name: 'Kevin Durant', team: 'the Thunder'},
{name: 'Kobe Jordan', team: 'the Lakers'}
];
var days = [
'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'
];
var data = {
basketballPlayers: basketballPlayers,
days: days
};
res.render('loop', data);
});
app.get('/logic', function(req, res){
var data = {
upIsUp: true,
downIsUp: false,
skyIsBlue: "yes"
};
res.render('logic', data);
});
app.listen(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment