fix(examples): fix express cluster worker error at startup
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 30 Mar 2024 20:34:39 +0000 (21:34 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 30 Mar 2024 20:34:39 +0000 (21:34 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
examples/typescript/http-server-pool/express-cluster/src/worker.ts
examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts

index da2e9ad7fc68abece6a9339730b5c0d049dee574..98ca45deea0be0c1dab6bb92ff558555d9eee271 100644 (file)
@@ -43,14 +43,16 @@ class ExpressWorker extends ClusterWorker<WorkerData, WorkerResponse> {
         .end()
     })
 
+    let listenerPort: number | undefined
     ExpressWorker.server = application.listen(port, () => {
+      listenerPort = (ExpressWorker.server.address() as AddressInfo).port
       console.info(
-        `⚡️[express server]: Express server is started in cluster worker at http://localhost:${port}/`
+        `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort}/`
       )
     })
     return {
       status: true,
-      port: (ExpressWorker.server.address() as AddressInfo).port
+      port: listenerPort ?? port
     }
   }
 
index 78a18d67f79eda45815b987bb9b3bafb6fe459e0..7ba9f374f5fa248012fab50afb72e508f8391de4 100644 (file)
@@ -70,14 +70,16 @@ ClusterWorkerResponse
         .catch(emptyFunction)
     })
 
+    let listenerPort: number | undefined
     ExpressWorker.server = application.listen(port, () => {
+      listenerPort = (ExpressWorker.server.address() as AddressInfo).port
       console.info(
-        `⚡️[express server]: Express server is started in cluster worker at http://localhost:${port}/`
+        `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort}/`
       )
     })
     return {
       status: true,
-      port: (ExpressWorker.server.address() as AddressInfo).port
+      port: listenerPort ?? port
     }
   }