-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
34 lines (30 loc) · 938 Bytes
/
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
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
const cluster = require('cluster');
const path = require('path');
const logger = require('morgan');
const port = 2030;
// const root = path.dirname( __dirname );
const cCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Create a worker for each CPU
for (let i = 0; i < cCPUs; i += 1) {
cluster.fork();
}
cluster.on('online', (worker) => {
console.log(`Worker ${worker.process.pid} is online.`);
});
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died.`);
});
} else {
const app = express();
app.use(logger('combined'));
app.use(compression());
app.use(express.static(path.join(__dirname, 'html')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'html', 'index.html'));
});
app.use(bodyParser).listen(port);
}