-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.69 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
require('dotenv').config();
const express = require('express');
// const https = require('https');
const http = require('http');
const { Server } = require('socket.io');
const cors = require('cors');
const sequelize = require('./db');
const fileUpload = require('express-fileupload');
const path = require('path');
const cookieParser = require('cookie-parser');
const router = require('./routes/index');
const errorHandler = require('./middleware/ErrorHandlingMiddleware');
const handlingSocketsEvents = require('./gateway/socketsGateway');
const PORT = process.env.PORT || 5000;
const corsOptions = {
origin: [process.env.CLIENT_URL],
credentials: true,
exposedHeaders: ['set-cookie'],
}
const app = express();
app.use(cors(corsOptions));
app.use(express.json());
app.use(cookieParser());
app.use(express.static(path.resolve(__dirname, 'static')));
app.use(fileUpload({}));
app.use('/api', router);
app.use(errorHandler);
// const server = https.createServer(app)
const server = http.createServer(app);
const socketIO = new Server(server, {
maxHttpBufferSize: 1e8,
cors: {
origin: [process.env.CLIENT_URL],
credentials: true
},
});
const startApp = async () => {
try {
sequelize.authenticate()
.then(() => console.log('Connected to PostgreSQL DB'))
.catch((error) => console.log(`Error PostgreSQL connected ${error}`));
// await sequelize.sync({ alter: true });
await sequelize.sync();
server.listen(PORT, () => console.log(`Server started on port ${PORT}`));
handlingSocketsEvents(socketIO);
} catch (error) {
console.log(error);
}
}
startApp();