-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathserver.js
105 lines (85 loc) · 2.9 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
var Promise = global.Promise || require('promise');
var express = require('express'),
exphbs = require('../../'), // "express-handlebars"
helpers = require('./lib/helpers');
var app = express();
// Create `ExpressHandlebars` instance with a default layout.
var hbs = exphbs.create({
helpers : helpers,
// Uses multiple partials dirs, templates in "shared/templates/" are shared
// with the client-side of the app (see below).
partialsDir: [
'shared/templates/',
'views/partials/'
]
});
// Register `hbs` as our view engine using its bound `engine()` function.
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// Middleware to expose the app's shared templates to the client-side of the app
// for pages which need them.
function exposeTemplates(req, res, next) {
// Uses the `ExpressHandlebars` instance to get the get the **precompiled**
// templates which will be shared with the client-side of the app.
hbs.getTemplates('shared/templates/', {
cache : app.enabled('view cache'),
precompiled: true
}).then(function (templates) {
// RegExp to remove the ".handlebars" extension from the template names.
var extRegex = new RegExp(hbs.extname + '$');
// Creates an array of templates which are exposed via
// `res.locals.templates`.
templates = Object.keys(templates).map(function (name) {
return {
name : name.replace(extRegex, ''),
template: templates[name]
};
});
// Exposes the templates during view rendering.
if (templates.length) {
res.locals.templates = templates;
}
setImmediate(next);
})
.catch(next);
}
app.get('/', function (req, res) {
res.render('home', {
title: 'Home'
});
});
app.get('/yell', function (req, res) {
res.render('yell', {
title: 'Yell',
// This `message` will be transformed by our `yell()` helper.
message: 'hello world'
});
});
app.get('/exclaim', function (req, res) {
res.render('yell', {
title : 'Exclaim',
message: 'hello world',
// This overrides _only_ the default `yell()` helper.
helpers: {
yell: function (msg) {
return (msg + '!!!');
}
}
});
});
app.get('/echo/:message?', exposeTemplates, function (req, res) {
res.render('echo', {
title : 'Echo',
message: req.params.message,
// Overrides which layout to use, instead of the defaul "main" layout.
layout: 'shared-templates',
partials: Promise.resolve({
echo: hbs.handlebars.compile('<p>ECHO: {{message}}</p>')
})
});
});
app.use(express.static('public/'));
app.listen(3000, function () {
console.log('express-handlebars example server listening on: 3000');
});