-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
52 lines (40 loc) · 1.2 KB
/
run.js
File metadata and controls
52 lines (40 loc) · 1.2 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
var domain = require("domain");
var watch = require('watch');
var http = require('http');
var port = process.env.PORT || 3000;
var host = process.env.IP || "0.0.0.0";
var obj = {};
obj.app = function(){};
var appFile = './app';
function clearCache(){
module.children = [];
for(var i in require.cache ) delete require.cache[i];
}
function loadApp(){
var loadDomain = domain.create();
loadDomain.on('error', function(err) { console.error(err.stack); });
loadDomain.run(function(){
require(appFile)(obj);
});
return loadDomain;
}
loadApp();
var willReload = false;
watch.watchTree( __dirname ,
{ filter: function(f,stats){ return !(/(.js|.json)$/).test(f) && stats.isFile(); } } ,
function (f, curr, prev) {
if (typeof f == "object" && prev === null && curr === null) { } else {
if(willReload) return; willReload = true;
setTimeout(function(){
willReload = false;
clearCache();
loadApp();
console.log("reload app");
},1000);
}
});
http.createServer(function(){
obj.app.apply(null, arguments);
}).listen( port , host , function(){
console.log('server listening on %s:%s' ,host, port);
});