forked from imageadhikari/Scholarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (46 loc) · 2.67 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
48
49
50
51
52
53
54
55
56
57
58
59
60
// MODULE IMPORTS ------------------------------------
const express = require("express"); // Express framework module
const dotenv = require("dotenv"); // Dotenv module for managing environment variable
const connectDB = require("./config/db"); // Mongoose module for integrating MongoDB
const socketio = require("socket.io"); //socket.io module
// DEV-DEPENDENCIES
// const morgan = require("morgan"); // Morgan module for logging API request objects
const cors = require("cors"); //CORS module for cross origin resource sharing i.e. between different ports in same ip
//////////////////////////////////////////////////////////////////////////////////////
////////// ! SERVER CONFIGS //////////
//////////////////////////////////////////////////////////////////////////////////////
dotenv.config({ path: "./config/config.env" }); // loading environment variables
connectDB(); // Database connection
const app = express(); // Express app
app.use("/public/upload", express.static("public/upload"));
app.use(express.json()); // Middleware for parsing requests into JSON format and store in req.body
if (process.env.NODE_ENV === "dev") app.use(morgan("dev")); // Logger for requests
app.use(cors()); // Cross origin enabler
//////////////////////////////////////////////////////////////////////////////////////
////////// ! ROUTES //////////
//////////////////////////////////////////////////////////////////////////////////////
const apiRouter = require("./api/routes/apiRoutes");
app.use("/api", apiRouter); // (@param1: route, @param2 handler)
//////////////////////////////////////////////////////////////////////////////////////
////////// ! SERVER //////////
//////////////////////////////////////////////////////////////////////////////////////
const PORT = process.env.PORT || process.argv[2] || 4000;
const server = app.listen(PORT, "0.0.0.0", () =>
console.log(`Server running at ${PORT}.`)
);
//////////////////////////////////////////////////////////////////////////////////////
////////// ! SOCKETS //////////
//////////////////////////////////////////////////////////////////////////////////////
const io = socketio(server);
//namespaces
const text = io.of("/text");
const video = io.of("/video");
// connection handlers
text.on("connection", (socket) =>
console.log(`Socket connection made. Namespace:Chat, ID: ${socket.id}`)
);
video.on("connection", (socket) =>
console.log(`Socket connection made. Namespace:VideoChat, ID: ${socket.id}`)
);
// sockets ---
module.exports = server;