-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (32 loc) · 1.11 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
const path = require('path');
const express = require('express');
const cluster = require('cluster');
const app = express();
// middleware to serve gzipped js file.
// app.get('*.js', function (req, res, next) {
// req.url = req.url + '.gz';
// res.set('Content-Encoding', 'gzip');
// next();
// });
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
if(cluster.isMaster){
const cores = require('os').cpus().length;
console.log('Master cluster setting up', cores, 'workers...');
for(let i = 0; i < cores; i++) cluster.fork();
cluster.on('online', (worker) => console.log('Worker', worker.process.pid, 'is online'));
cluster.on('exit', (worker, code, signal) => {
console.log('Worker', worker.process.pid, 'died with code:', code, 'and signal:', signal);
console.log('Starting a new worker');
cluster.fork();
});
}else {
app.listen(process.env.PORT || 8080, function(err) {
if (err) {
return console.error(err);
}
console.log('Listening at http://localhost:' + (process.env.PORT || 8080));
});
}