forked from Reddit-platform-clone/BackEnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (38 loc) · 1.24 KB
/
server.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
47
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const { swaggerUi, specs, router } = require('./swaggerConfig');
const { app, server } = require("./utils/WebSockets");
const fileupload = require('express-fileupload');
app.use(fileupload({useTempFiles: true}))
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB!');
});
const PORT = process.env.PORT || 5000;
app.use(express.json())
app.use(bodyParser.json());
app.use(cors());
app.use('/api-docs', router);
function loadRoutes(directory) {
fs.readdirSync(directory).forEach((file) => {
const filePath = path.join(directory, file);
const route = require(filePath);
app.use('/', route);
});
}
const routesDirectory = path.join(__dirname, 'routes');
loadRoutes(routesDirectory);
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`WebSocket server is running on port ${server.address().port}`);
});
module.exports = app;