-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
29 lines (23 loc) · 757 Bytes
/
index.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
const dotenv = require("dotenv");
const cluster = require("node:cluster");
const http = require("node:http");
const numCPUs = require("node:os").cpus().length;
const process = require("node:process");
dotenv.config();
const PORT = process.env.PORT || 3001;
const app = require('./app')
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) cluster.fork();
cluster.on("exit", (worker, code, signal) =>
console.log(`worker ${worker.process.pid} died`)
);
} else {
http
.createServer(app)
.listen(PORT, err => {
if(err) throw new Error(err);
console.log(`Server Running => http://localhost:${PORT}`);
});
console.log(`\nWorker ${process.pid} started`);
}