-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (33 loc) · 1.26 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
const http = require('http');
const app = require('./src/app');
const keys = require('./src/config/keys');
const mongoose = require('mongoose');
const os = require('os');
const cluster = require('cluster');
const clusterWorkerSize = os.cpus().length;
const server = new http.Server(app);
const PORT = process.env.PORT || 5001;
mongoose
.connect(keys.mongoConnectionString, { useNewUrlParser: true })
.then(result => {
console.log('Connected to database');
if (clusterWorkerSize < 1) { //change to > to run on all cores
if (cluster.isMaster) {
for (let i = 0; i < clusterWorkerSize; i++) {
cluster.fork();
}
cluster.on('exit', function(worker) {
console.log('Worker', worker.id, ' has exitted.');
});
} else {
server.listen(PORT, function() {
console.log(`Express server listening on port ${PORT} and worker ${process.pid}`);
});
}
} else {
server.listen(PORT, function() {
console.log(`Express server listening on port ${PORT} with the single worker ${process.pid}`);
});
}
})
.catch(err => console.log(err));