-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 1.78 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
console.clear();
/**
* Configure Environment Variables
*/
require("dotenv").config();
/**
* Import the dependencies
*/
const chalk = require("chalk");
const path = require("path");
console.log(chalk.cyanBright(">>===> Starting the application..."));
/**
* Create the server
*/
const express = require("express");
const app = express();
const http = require("http");
const https = require("https");
const server = process.env.NODE_ENV === "production" ? https.createServer(app) : http.createServer(app);
const io = require("socket.io")(server, {
cookie: true,
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
require('./socket/main')(io);
/**
* Connect to the database
*/
const database = require("./database");
/**
* Install All Middlewares
*/
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors({
origin: "http://localhost:3000",
credentials: true
}, {
origin: "*",
credentials: true
}));
app.use(express.static('/client/build'));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
/**
* Install All Routes
*/
app.use("/api", require(path.resolve(__dirname, "./Router", "./api", "./api.js")));
/**
* Start the server
*/
const port = process.env.PORT || 5000;
database.connect(process.env.MONGODB_URI, (err) => {
server.listen(port, (err) => {
if (err) {
console.error(chalk.redBright(">>===> An error occured while starting the server"), err);
} else {
console.log(chalk.greenBright(`>>===> Server started on port ${port}`));
}
});
});