-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (39 loc) · 1.59 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require('dotenv').config(); // only needed to require it here
const express = require('express');
const userRoutes = require('./routes/userRoutes');
const gameRoutes = require('./routes/gameRoutes');
const rankingRoutes = require('./routes/rankingRoutes');
const notFoundController = require('./middlewares/notFoundController');
// mysql imports_______________________________________________________________
const { initDB } = require('./models/initModels');
// MongoDB imports_____________________________________________________________
const { connectMongoDB } = require('./db/createMongoDB');
const app = express();
// Enable json parsing
app.use(express.json());
app.use('/users', userRoutes);
app.use('/games', gameRoutes);
app.use('/ranking', rankingRoutes);
app.use(notFoundController);
// Strategy is to 1ST connect to ANY database and THEN start LISTENING.
// Connect to mysql database if chosen
if (process.env.DB === 'mysql') {
console.log('#################');
console.log('# ' + process.env.DB + ' #');
console.log('#################');
const PORT = process.env.PORT || 5000;
initDB().then(() => app.listen(PORT, () => console.log('Server is running of port ' + PORT)));
}
// Connect to mongoDB database if chosen
if (process.env.DB === 'mongodb') {
console.log('#################');
console.log('# ' + process.env.DB + ' #');
console.log('#################');
connectMongoDB((error) => {
if (!error) {
const PORT = 5000;
app.listen(PORT, () => console.log(`Server is listening to port ${PORT}`));
console.log('Connected to the database');
}
});
}