-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (37 loc) · 1.26 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
var express = require('express'),
cheerio = require('cheerio'),
fs = require('fs'),
app = express();
// Use embedded javascript for the view engine (templates)
app.set('view engine', 'ejs');
// Allow relative image links from either ./dist/img or ./src/img
app.use("/src/img", express.static(__dirname + "/src/img"));
app.use("/dist/img", express.static(__dirname + "/dist/img"));
// Set the route handler for the preview page.
app.get('/',function(req,res){
res.status(200);
var data = {
templates: getTemplates()
};
res.render(__dirname + '/preview/index', data);
});
module.exports = app;
// Helper function to get templates and their "subject" from <title> tag
function getTemplates() {
var templates = [],
templateDir = __dirname + '/dist/',
templateFiles = fs.readdirSync(templateDir);
templateFiles.forEach( function (file) {
if (file.substr(-5) === '.html') {
var contents = fs.readFileSync(templateDir + file, 'utf8');
if (contents) {
$ = cheerio.load(contents);
templates.push({
'filename': file,
'subject': $("html title").text() || "Subject not available"
});
}
}
});
return templates;
}