-
Notifications
You must be signed in to change notification settings - Fork 33
/
server.js
55 lines (46 loc) · 1.33 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
// Dependencies:
var createServer = require('http').createServer;
var staticServer = require('node-static').Server;
var webmake = require('webmake');
// Project path:
var projectPath = __dirname;
// Public folder path (statics)
var staticsPath = projectPath;
// Server port:
var port = 8000;
staticServer = new staticServer(staticsPath);
var bundles = {
'/js/all.js': __dirname + '/src/bits.js'
};
// Initialize http server
createServer(function (req, res) {
// Start the flow (new Stream API demands that)
req.resume();
// Respond to request
req.on('end', function () {
if ( bundles[req.url] ) {
// Generate bundle with Webmake
var programPath = bundles[ req.url ];
// Send headers
res.writeHead(200, {
'Content-Type': 'application/javascript; charset=utf-8',
// Do not cache generated bundle
'Cache-Control': 'no-cache'
});
var time = Date.now();
webmake(bundles[req.url], { cache: true }, function (err, content) {
if (err) {
console.error("Webmake error: " + err.message);
res.end('console.error("Webmake error: ' + err.message + '");');
return;
}
// Send script
console.log("Webmake OK (" + ((Date.now() - time)/1000).toFixed(3) + "s)");
res.end(content);
});
} else {
staticServer.serve(req, res);
}
});
}).listen(port);
console.log("Server started");