-
Notifications
You must be signed in to change notification settings - Fork 7
/
index-express.js
102 lines (83 loc) · 4.09 KB
/
index-express.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
'use strict'
// Rendering JsRender templates using Express 4
// Use this file as start file, after installing Express
var Express = require('express');
var Fs = require('fs');
var app = Express();
app.set('port', (process.env.PORT || 5000));
var favicon = require('serve-favicon');
app.use(favicon(__dirname + '/public/images/logo-jsv.png'));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Set public directory as root for static files,
// and also enable getting jsrender.js and jsviews.js as static files from node_modules folders
app.use(Express.static(__dirname + '/public'));
app.use(Express.static(__dirname + '/node_modules/jsrender'));
app.use(Express.static(__dirname + '/node_modules/jsviews'));
// Load JsRender library
var jsrender = require('jsrender');
////////////////////////////////////////////////////////////////
// Set JsRender as the template engine for Express
app.engine('html', jsrender.__express);
app.set('view engine', 'html');
// We will store JsRender templates for Express in the templates folder
app.set('views', __dirname + '/templates');
////////////////////////////////////////////////////////////////
// Load the current data for movies - stored in the file system
var movieData = Fs.readFileSync('./public/data/movies.json');
var appData = {movies: JSON.parse(movieData)};
// Provide the bgColor helper, used by the movie-list template for striped rows. (
jsrender.views.helpers('bgColor', function() {
return this.index%2 ? '#fdfdfe' : '#efeff2';
}); // We could optionally have exposed the helper only to the the movie-list template - by passing
// in the template - jsrender.templates('./templates/moviespage.html') - as third parameter.
// (Note that JsRender server templates are automatically cached - so no additional call is made to the file-system)
// Define custom tags used in layout-movies.html template
jsrender.views.tags({
clientData: function(varName) { // Custom tag to render server data in a script block, so it can be used as in the client without making an HTTP request
return '<script>var ' + varName + '=' + JSON.stringify(appData[varName]) + ';</script>';
}
});
////////////////////////////////////////////////////////////////
// Render layout-movies.html template as 'home' page using Express
app.get('/', function(req, res) { // Express template
res.render('layout-movies', appData, function(err, html) {
res.send(html);
});
});
////////////////////////////////////////////////////////////////
// Render layout-hello.html template as 'hello/world' page - without using Express
app.get('/hello/world', function(req, res) {
// Load template from file system, and render against data
var html = jsrender.renderFile('./templates/layout-hello.html', { hello: "world" });
// Alternatively:
// Use jsrender file path support to compile template from file, then render
// var tmpl = jsrender.templates('./templates/layout-hello.html');
// html = tmpl.render({ hello: "world" });
res.send(html);
});
////////////////////////////////////////////////////////////////
// Render layout-hello2.html template as 'hello/world2' page - without using Express
app.get('/hello/world2', function(req, res) {
// Load template from file system, and render against data
var html = jsrender.renderFile('./templates/layout-hello2.html', { hello: "world" });
// Alternatively:
// Use jsrender file path support to compile template from file, then render
// var tmpl = jsrender.templates('./templates/layout-hello.html');
// html = tmpl.render({ hello: "world" });
res.send(html);
});
////////////////////////////////////////////////////////////////
// Save updated data from client, received from POST request
app.post('/save/data', function(req, res) {
movieData = req.body.movieData;
appData.movies = JSON.parse(movieData);
Fs.writeFileSync('./public/data/movies.json', movieData);
res.send("Saved...");
});
////////////////////////////////////////////////////////////////
// Start server
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});