@@ -13,7 +13,14 @@ require('./lib/feature-flags')
1313const { PORT , NODE_ENV } = process . env
1414const port = Number ( PORT ) || 4000
1515
16- function main ( ) {
16+ // Start the server!
17+ if ( NODE_ENV === 'production' ) {
18+ clusteredMain ( )
19+ } else {
20+ nonClusteredMain ( )
21+ }
22+
23+ function clusteredMain ( ) {
1724 // Spin up a cluster!
1825 throng ( {
1926 master : setupPrimary ,
@@ -22,8 +29,40 @@ function main () {
2229 } )
2330}
2431
25- // Start the server!
26- main ( )
32+ async function nonClusteredMain ( ) {
33+ await checkPortAvailability ( )
34+ await startServer ( )
35+ }
36+
37+ async function checkPortAvailability ( ) {
38+ // Check that the development server is not already running
39+ const portInUse = await portUsed . check ( port )
40+ if ( portInUse ) {
41+ console . log ( `\n\n\nPort ${ port } is not available. You may already have a server running.` )
42+ console . log ( 'Try running `killall node` to shut down all your running node processes.\n\n\n' )
43+ console . log ( '\x07' ) // system 'beep' sound
44+ process . exit ( 1 )
45+ }
46+ }
47+
48+ async function startServer ( ) {
49+ const app = require ( './lib/app' )
50+ const warmServer = require ( './lib/warm-server' )
51+
52+ // If in a deployed environment...
53+ if ( NODE_ENV === 'production' ) {
54+ // If in a true production environment, wait for the cache to be fully warmed.
55+ if ( process . env . HEROKU_PRODUCTION_APP || process . env . GITHUB_ACTIONS ) {
56+ await warmServer ( )
57+ }
58+ }
59+
60+ // Workaround for https://github.com/expressjs/express/issues/1101
61+ const server = require ( 'http' ) . createServer ( app )
62+ server
63+ . listen ( port , ( ) => console . log ( `app running on http://localhost:${ port } ` ) )
64+ . on ( 'error' , ( ) => server . close ( ) )
65+ }
2766
2867// This function will only be run in the primary process
2968async function setupPrimary ( ) {
@@ -34,14 +73,7 @@ async function setupPrimary () {
3473
3574 console . log ( 'Starting up primary...' )
3675
37- // Check that the development server is not already running
38- const portInUse = await portUsed . check ( port )
39- if ( portInUse ) {
40- console . log ( `\n\n\nPort ${ port } is not available. You may already have a server running.` )
41- console . log ( 'Try running `killall node` to shut down all your running node processes.\n\n\n' )
42- console . log ( '\x07' ) // system 'beep' sound
43- process . exit ( 1 )
44- }
76+ await checkPortAvailability ( )
4577}
4678
4779// IMPORTANT: This function will be run in a separate worker process!
@@ -64,22 +96,7 @@ async function setupWorker (id, disconnect) {
6496 console . log ( 'Starting up worker...' )
6597
6698 // Load the server in each worker process and share the port via sharding
67- const app = require ( './lib/app' )
68- const warmServer = require ( './lib/warm-server' )
69-
70- // If in a deployed environment...
71- if ( NODE_ENV === 'production' ) {
72- // If in a true production environment, wait for the cache to be fully warmed.
73- if ( process . env . HEROKU_PRODUCTION_APP || process . env . GITHUB_ACTIONS ) {
74- await warmServer ( )
75- }
76- }
77-
78- // Workaround for https://github.com/expressjs/express/issues/1101
79- const server = require ( 'http' ) . createServer ( app )
80- server
81- . listen ( port , ( ) => console . log ( `app running on http://localhost:${ port } ` ) )
82- . on ( 'error' , ( ) => server . close ( ) )
99+ await startServer ( )
83100
84101 function shutdown ( ) {
85102 if ( exited ) return
0 commit comments