forked from claudusd/edu-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (71 loc) · 2.29 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
76
77
78
79
80
81
var chokidar = require('chokidar');
var child_process = require('child_process');
var sys = require("util");
var server = {
process: null,
restarting: false,
start: function() {
console.log('Starting server');
var that = this;
this.process = child_process.spawn('node', ['src/server/index.js']);
this.process.stdout.addListener('data', function (data) {
process.stdout.write(data);
});
this.process.stderr.addListener('data', function (data) {
sys.print(data);
});
this.process.addListener('exit', function (code) {
console.log('Child process exited: ' + code);
this.process = null;
if (that.restarting) {
that.restarting = false;
that.start();
}
});
},
watch: function() {
var watcher = chokidar.watch('src/server', {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true
});
this.start();
var that = this;
watcher
.on('add', function(path) { that.restart(path); })
.on('change', function(path) { that.restart(path); })
.on('unlink', function(path) { that.restart(path); });
const port = 4000;
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const config = require('./webpack.config');
new WebpackDevServer(webpack(config), {
hot: true,
historyApiFallback: true,
publicPath: '/',
proxy: {
'/notes/*': {
target: 'http://localhost:3000',
}
}
}).listen(port, 'localhost', function (err) {
if (err) {
return console.log(err);
}
console.log(`Development server running at http://localhost:${port}`);
});
},
restart: function(path) {
if (path.match('\.js$')) {
this.restarting = true;
console.log('Stopping server for restart');
this.process.kill();
}
}
};
if ('dev' === process.env.NODE_ENV) {
server.watch();
}
if ('production' === process.env.NODE_ENV) {
server.start();
}