-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (41 loc) · 1.34 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
43
44
45
46
47
48
49
50
51
const express = require("express");
const cluster = require("cluster");
const os = require("os");
const app = express();
const delay = (duration) => {
const startTime = Date.now();
while (Date.now() - startTime < duration) {
//event loop is blocked
}
};
app.get("/", (req, res) => {
res.send(`Performance example: ${process.pid} `);
});
app.get("/timer", (req, res) => {
delay(9000);
res.send(`Ding ding ding: ${process.pid}`);
});
//cluster is a module that allows us to create multiple instances of our server
//and distribute the load across them
// if (cluster.isMaster) {
// console.log("Master process is running");
//os.cpus() returns the number of cores in our machine
// let NoOfWorkers = os.cpus().length;
// console.log(`No of workers: ${NoOfWorkers}`);
//fork() is a method that creates a new instance of our server
// for (let i = 0; i < NoOfWorkers; i++) {
// cluster.fork();
// cluster.fork();
// }
// } else {
// console.log("Worker process is running");
// app.listen(3000, () => {
// console.log("Server is running on port 3000");
// });
// }
// pm2 is a module that allows us to run our server in cluster mode
// pm2 start server.js -i 0
// -i 0 means that pm2 will create as many instances as there are cores in our machine
app.listen(3000, () => {
console.log("Server is running on port 3000");
});