Skip to content

Commit

Permalink
Merge pull request #3223 from processing/task/mongodb-connection
Browse files Browse the repository at this point in the history
Task/mongodb connection
  • Loading branch information
raclim authored Aug 22, 2024
2 parents f1b2d1b + e31afef commit 7cec8e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
7 changes: 5 additions & 2 deletions server/previewServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ const mongoConnectionString = process.env.MONGO_URL;
// Connect to MongoDB
const connectToMongoDB = async () => {
try {
mongoose.set('strictQuery', true);

await mongoose.connect(mongoConnectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
socketTimeoutMS: 45000 // 45 seconds timeout
});
} catch (error) {
console.error('Failed to connect to MongoDB: ', error);
Expand All @@ -31,7 +35,6 @@ const connectToMongoDB = async () => {

connectToMongoDB();

mongoose.set('strictQuery', true);
mongoose.connection.on('error', () => {
console.error(
'MongoDB Connection Error. Please make sure that MongoDB is running.'
Expand Down
37 changes: 13 additions & 24 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ app.options('*', corsMiddleware);
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(cookieParser());

mongoose.set('strictQuery', true);

const clientPromise = mongoose
.connect(mongoConnectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000, // 30 seconds timeout
socketTimeoutMS: 45000 // 45 seconds timeout
})
.then((m) => m.connection.getClient());

app.use(
session({
resave: true,
Expand All @@ -85,7 +97,7 @@ app.use(
secure: false
},
store: new MongoStore({
mongooseConnection: mongoose.connection,
clientPromise,
autoReconnect: true
})
})
Expand Down Expand Up @@ -151,29 +163,6 @@ app.use('/', passportRoutes);
// configure passport
require('./config/passport');

// Connect to MongoDB
const connectToMongoDB = async () => {
try {
await mongoose.connect(mongoConnectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
});
} catch (error) {
console.error('Failed to connect to MongoDB: ', error);
process.exit(1);
}
};

connectToMongoDB();

mongoose.set('strictQuery', true);
mongoose.connection.on('error', () => {
console.error(
'MongoDB Connection Error. Please make sure that MongoDB is running.'
);
process.exit(1);
});

app.get('/', (req, res) => {
res.sendFile(renderIndex());
});
Expand Down

0 comments on commit 7cec8e5

Please sign in to comment.