@@ -73,6 +73,18 @@ app.options('*', corsMiddleware);
7373app . use ( bodyParser . urlencoded ( { limit : '50mb' , extended : true } ) ) ;
7474app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
7575app . use ( cookieParser ( ) ) ;
76+
77+ mongoose . set ( 'strictQuery' , true ) ;
78+
79+ const clientPromise = mongoose
80+ . connect ( mongoConnectionString , {
81+ useNewUrlParser : true ,
82+ useUnifiedTopology : true ,
83+ serverSelectionTimeoutMS : 30000 , // 30 seconds timeout
84+ socketTimeoutMS : 45000 // 45 seconds timeout
85+ } )
86+ . then ( ( m ) => m . connection . getClient ( ) ) ;
87+
7688app . use (
7789 session ( {
7890 resave : true ,
@@ -85,7 +97,7 @@ app.use(
8597 secure : false
8698 } ,
8799 store : new MongoStore ( {
88- mongooseConnection : mongoose . connection ,
100+ clientPromise ,
89101 autoReconnect : true
90102 } )
91103 } )
@@ -151,29 +163,6 @@ app.use('/', passportRoutes);
151163// configure passport
152164require ( './config/passport' ) ;
153165
154- // Connect to MongoDB
155- const connectToMongoDB = async ( ) => {
156- try {
157- await mongoose . connect ( mongoConnectionString , {
158- useNewUrlParser : true ,
159- useUnifiedTopology : true
160- } ) ;
161- } catch ( error ) {
162- console . error ( 'Failed to connect to MongoDB: ' , error ) ;
163- process . exit ( 1 ) ;
164- }
165- } ;
166-
167- connectToMongoDB ( ) ;
168-
169- mongoose . set ( 'strictQuery' , true ) ;
170- mongoose . connection . on ( 'error' , ( ) => {
171- console . error (
172- 'MongoDB Connection Error. Please make sure that MongoDB is running.'
173- ) ;
174- process . exit ( 1 ) ;
175- } ) ;
176-
177166app . get ( '/' , ( req , res ) => {
178167 res . sendFile ( renderIndex ( ) ) ;
179168} ) ;
0 commit comments