This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (83 loc) · 2.7 KB
/
server.js
File metadata and controls
96 lines (83 loc) · 2.7 KB
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
var
express = require('express'),
path = require('path'),
fs = require('fs'),
util = require('util'),
mu = require('mu2');
mu.root = path.join(__dirname, 'components');
var view = {
ASSET_HOST: process.env.ASSET_HOST ? process.env.ASSET_HOST : "//localhost:" + process.env.PORT
};
var app = express();
app.use(function(req, res, next) {
// remove for security-by-obscurity for automated attacks
res.removeHeader("x-powered-by");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
if (process.env.DISABLE_CACHE){
app.use(function(req, res, next) {
mu.clearCache();
next();
});
}
app.use('/assets', express.static(path.join(__dirname, 'assets')));
app.use('/components', express.static(path.join(__dirname, 'components')));
app.use(express.bodyParser());
app.use(express.logger());
app.get('/package/:components', function (req, res) {
var components = req.params.components.split('+');
var componentData = {};
function collectAndSendComponentData () {
var packagedData = '';
if (components.length) {
var todo = components.length;
components.forEach(function (name) {
componentData[name] = '';
mu.compileAndRender(name, view)
.on('data', function (dataFragment) {
componentData[name] += dataFragment.toString();
})
.on('end', function () {
if (--todo === 0) {
res.write('<elements>\n');
components.forEach(function (c) {
res.write(componentData[c]);
});
res.write('</elements>\n');
res.end();
}
})
});
}
else {
res.send('', 200);
}
}
if (components.length === 1 && components[0] === 'all') {
fs.readdir(path.join(__dirname, 'components'), function (err, files) {
components = files;
// MAKE SURE these are sorted alphabetically
components = components.sort(function (a, b) {
return a < b ? -1 : (a > b ? 1 : 0);
});
collectAndSendComponentData();
});
}
else {
components = components.map(function (name) {
return name + '.html';
});
collectAndSendComponentData();
}
});
if (process.env.ENV === 'development') {
app.use('/tests', express.static(path.join(__dirname, 'tests')));
app.use('/tests', express.static(path.join(__dirname, 'node_modules', 'mocha')));
app.use('/tests', express.static(path.join(__dirname, 'node_modules', 'expect')));
app.use('/tests', express.static(path.join(__dirname, 'external', 'ceci')));
}
var server = app.listen(process.env.PORT, function (err) {
console.log('Running on', server.address());
});