Skip to content

Commit 0fda27a

Browse files
committed
update mongodb mongoose connection to match new syntax
1 parent 15e3548 commit 0fda27a

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

server/previewServer.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,27 @@ const app = new Express();
1616
// but i have no idea why
1717
const mongoConnectionString = process.env.MONGO_URL;
1818

19-
// Connect to MongoDB
2019
const connectToMongoDB = async () => {
2120
try {
2221
mongoose.set('strictQuery', true);
2322

2423
await mongoose.connect(mongoConnectionString, {
25-
useNewUrlParser: true,
26-
useUnifiedTopology: true,
27-
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
28-
socketTimeoutMS: 45000 // 45 seconds timeout
24+
serverSelectionTimeoutMS: 30000,
25+
socketTimeoutMS: 45000
2926
});
3027
} catch (error) {
31-
console.error('Failed to connect to MongoDB: ', error);
28+
console.error('Failed to connect to MongoDB:', error);
3229
process.exit(1);
3330
}
3431
};
3532

3633
connectToMongoDB();
3734

38-
mongoose.connection.on('error', () => {
35+
mongoose.connection.on('error', (err) => {
3936
console.error(
40-
'MongoDB Connection Error. Please make sure that MongoDB is running.'
37+
'MongoDB Connection Error. Please make sure that MongoDB is running.',
38+
err
4139
);
42-
process.exit(1);
4340
});
4441

4542
const allowedCorsOrigins = [

server/server.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import bodyParser from 'body-parser';
44
import cookieParser from 'cookie-parser';
55
import cors from 'cors';
66
import session from 'express-session';
7-
import connectMongo from 'connect-mongo';
7+
import MongoStore from 'connect-mongo';
88
import passport from 'passport';
99
import path from 'path';
1010
import basicAuth from 'express-basic-auth';
@@ -32,7 +32,6 @@ import { renderIndex } from './views/index';
3232
import { get404Sketch } from './views/404Page';
3333

3434
const app = new Express();
35-
const MongoStore = connectMongo(session);
3635

3736
app.get('/health', (req, res) => res.json({ success: true }));
3837

@@ -76,14 +75,12 @@ app.use(cookieParser());
7675

7776
mongoose.set('strictQuery', true);
7877

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());
78+
// TODO: update mongodb connection for other scripts
79+
80+
mongoose.connect(mongoConnectionString, {
81+
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
82+
socketTimeoutMS: 45000 // 45 seconds timeout
83+
});
8784

8885
app.use(
8986
session({
@@ -97,9 +94,9 @@ app.use(
9794
secure: false,
9895
maxAge: 1000 * 60 * 60 * 24 * 28 // 4 weeks in milliseconds
9996
},
100-
store: new MongoStore({
101-
clientPromise,
102-
autoReconnect: true
97+
store: MongoStore.create({
98+
mongoUrl: mongoConnectionString,
99+
ttl: 1000 * 60 * 60 * 24 * 28 // 4 weeks in milliseconds to match cookie maxAge
103100
})
104101
})
105102
);

0 commit comments

Comments
 (0)