forked from longmenwaideyu/expressjs-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (49 loc) · 1.23 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
var fs = require('fs');
var files = [
'routes',
'routes/blog',
'routes/collect',
'common',
'models',
'app.js',
'useRoutes.js'
];//被监视的文件列表
var stamps = {};//被监视文件的时间戳
var change = [];
var child = null;
function startServer() {
var spawn = require('child_process').spawn;
child = spawn('node', ['./bin/www']);
child.stdout.on('data', function (data) {
console.log(data.toString());
});
child.stderr.on('data', function (data) {
console.log(data.toString());
});
child.on('exit', function (code) {
console.log('子进程已关闭');
child = startServer();
});
child.on('error', function(code, signal){
child.kill(signal);
});
return child;
}
startServer();
///*
setInterval(function () {
change = [];
for (var i = files.length - 1; i >= 0; i--) {
var stat = fs.statSync(files[i]);//读取文件信息
var t = + new Date(stat.mtime);//mtime是修改时间
if (stamps[files[i]] && t > stamps[files[i]]) {
change.push(files[i]);
}
stamps[files[i]] = t;
}
if (change.length) {
console.log("重启");
child.kill();
}
}, 2000);
//*/