-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (55 loc) · 1.68 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
61
62
63
64
65
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const PORT = process.env.PORT || 8000;
const { createServer } = require("http");
const { Server } = require("socket.io");
//for handling any uncaught exceptions from my side
process.on("uncaughtException", (err) => {
console.log(`UNCAUGHT EXCEPTION! 💥 Shutting down...`);
console.log(err.name, err.message);
process.exit(1);
});
//initializing environment variables
dotenv.config({ path: "./.env" });
const app = require("./app.js");
const httpServer = createServer(app);
//connecting to our database
const DB = process.env.DATABASE.replace(
"<password>",
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(console.log("DB Connection Successful"));
//initializing socket instance and configuring cors
const io = new Server(httpServer, {
cors: {
origin: ["http://localhost:3000", "http://localhost:3001"],
},
});
//handling all socket requestion
app.use(require("./controllers/SocketArchitecture/Websockets")(io));
//we start listening to incoming requests
httpServer.listen(PORT, () => {
console.log(`App running on port ${PORT}...`);
});
//for any errors outside express
process.on("unhandledRejection", (err) => {
console.log(err.name, err.message);
console.log(`UNHANDLED REJECTION! 💥 Shutting down...`);
httpServer.close(() => {
process.exit(1);
});
});
//for heroku shutdowns
process.on("SIGTERM", () => {
console.log(`SIGTERM FROM HEROKU RECEIVED! Shutting down gracefully...`);
httpServer.close(() => {
console.log(`💥 Process terminated`);
});
});