-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·85 lines (71 loc) · 2.32 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
77
78
79
80
81
82
83
84
85
// Import Packages
const cors = require("cors");
const path = require("path");
const morgan = require("morgan");
const rateLimit = require("express-rate-limit");
const mongoSanitize = require("express-mongo-sanitize")
// Server INIT
const http = require("http");
const express = require("express");
const app = express();
const server = http.createServer(app);
// Middlewares
app.use(cors());
app.use(express.json());
app.use(mongoSanitize());
app.use(morgan("common"));
// Apply rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: "Too many requests, please try again later.",
});
app.use(limiter);
// Websocket INIT
const socket = require("socket.io");
const io = socket(server);
// Import Websocket modules
require("./socket/socket")(io);
// Import routes
const userRoute = require("./routes/users");
const authRoute = require("./routes/auth");
const postRoute = require("./routes/posts");
const uploadRoute = require("./routes/upload");
const commentRoute = require("./routes/comments");
const messageRoute = require("./routes/messages");
const conversationRoute = require("./routes/conversations");
// Dotenv configuration
require("dotenv").config();
// Database connection
const connectOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
};
require("mongoose").connect(process.env.DB_URL, connectOptions, () => {
console.log("Connected to DB");
});
// Multimedia Middlewares
app.use("/images", express.static(path.join(__dirname, "public/images")));
app.use("/voices", express.static(path.join(__dirname, "public/voices")));
// Routes Middlewares
app.use("/api/v1/auth", authRoute);
app.use("/api/v1/users", userRoute);
app.use("/api/v1/posts", postRoute);
app.use("/api/v1/upload", uploadRoute);
app.use("/api/v1/messages", messageRoute);
app.use("/api/v1/comments", commentRoute);
app.use("/api/v1/conversations", conversationRoute);
// For production
if (process.env.PROD) {
app.use(express.static(path.join(__dirname, "./client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
}
// Server listening
let port = process.env.PORT || 8800;
server.listen(port, () => {
console.log(`Server is running on ${port}`);
});