-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (38 loc) · 985 Bytes
/
app.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
'use strict';
//Always Necessary
const express = require("express"),
bodyParser = require("body-parser"),
cluster = require('cluster'),
cpuCount = require('os').cpus().length;
//routes
const state = require('./routes/state');
if(cluster.isMaster){
for(let i = 0; i < cpuCount; i++){
cluster.fork();
}
cluster.on('online', (worker) =>{
console.log(`Worker: ${worker.process.pid} has been spawned`)
})
cluster.on('exit', (worker) =>{
console.log(`Worker ${worker.process.pid} died, new fork`)
cluster.fork();
})
} else {
//express configs
const port = 5555;
const app = express();
//middleware
app.use(bodyParser.text())
//routes
app.use("/api/population", state);
app.use("*", (_, res) => {res.status(400).json({message:"Invalid API"})})
//listener
app.listen(port, (err) => {
if(!err){
console.log(`listening on port ${port}`)
}
else{
console.log(`Error ${err} occured when trying to start server`)
}
});
}