This is how I understand it: When the file starts to be executed cluster.isMaster is true Then enter the true branch to fork Create a new thread for each fork. This thread will execute the file from the beginning At this time cluster.isMaster is false Enter false branch Execute to create the server As for why the same port is listened to In fact, only the main thread listens to this port cluster The main thread will implement the distributor distribute the request to other threads on this port
Created and listened to the port by the master process. When the request reaches the server master process, it accepts it and distributes it to the worker process
This is how I understand it:
When the file starts to be executed
cluster.isMaster is true
Then enter the true branch to fork
Create a new thread for each fork. This thread will execute the file from the beginning
At this time cluster.isMaster is false
Enter false branch
Execute to create the server
As for why the same port is listened to
In fact, only the main thread listens to this port
cluster The main thread will implement the distributor
distribute the request to other threads on this port
Created and listened to the port by the master process. When the request reaches the server master process, it accepts it and distributes it to the worker process