-
Notifications
You must be signed in to change notification settings - Fork 339
/
server.js
75 lines (57 loc) · 2.06 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
var express = require('express'),
cheerio = require('cheerio'),
path = require('path'),
fs = require('fs'),
app = express();
// Use embedded javascript for the view engine (templates)
app.set('view engine', 'ejs');
// Routes to assets
app.use('/', express.static(path.join(__dirname, '/preview')));
app.use('/css', express.static(path.join(__dirname, '/preview/css')));
app.use('/dist', express.static(path.join(__dirname, '/dist')));
// Allow relative image links from either ./dist/img or ./src/img
app.use('/src/img', express.static(path.join(__dirname, '/src/img')));
app.use('/dist/img', express.static(path.join(__dirname, '/dist/img')));
// DEBUG - load CSS directly for inspection
// app.use('/src/css', express.static(path.join(__dirname, '/src/css')));
app.use(require('connect-livereload')({
port: 35729
}));
app.listen(process.env.PORT, function() {
console.log('Express server listening.');
});
// Set the route handler for the preview page.
app.get('/', (req, res) => {
res.status(200);
var data = {
templates: getTemplates()
};
res.render(path.join(__dirname,'/preview/index'), data);
});
module.exports = app;
// DEBUG - custom callback for simple server logging
/*
module.exports = app.listen(4000, function() {
console.log('Express server listening on port ' + app.get('port'));
});
*/
// Helper function to get templates and their 'subject' from <title> tag
function getTemplates() {
var templates = [],
templateDir = path.join(__dirname,'/dist/'),
templateFiles = fs.readdirSync(templateDir);
templateFiles.forEach( function (file) {
// if (file.substr(-5) === '.html') {
if (file.substring(file.length -5) === '.html') {
var contents = fs.readFileSync(templateDir + file, 'utf8');
if (contents) {
$ = cheerio.load(contents);
templates.push({
'filename': '/dist/' + file,
'subject': $('html title').text() || 'Subject not available'
});
}
}
});
return templates;
}