@@ -73,6 +73,18 @@ app.options('*', corsMiddleware);
73
73
app . use ( bodyParser . urlencoded ( { limit : '50mb' , extended : true } ) ) ;
74
74
app . use ( bodyParser . json ( { limit : '50mb' } ) ) ;
75
75
app . 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
+
76
88
app . use (
77
89
session ( {
78
90
resave : true ,
@@ -85,7 +97,7 @@ app.use(
85
97
secure : false
86
98
} ,
87
99
store : new MongoStore ( {
88
- mongooseConnection : mongoose . connection ,
100
+ clientPromise ,
89
101
autoReconnect : true
90
102
} )
91
103
} )
@@ -151,29 +163,6 @@ app.use('/', passportRoutes);
151
163
// configure passport
152
164
require ( './config/passport' ) ;
153
165
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
-
177
166
app . get ( '/' , ( req , res ) => {
178
167
res . sendFile ( renderIndex ( ) ) ;
179
168
} ) ;
0 commit comments