-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (85 loc) · 2.49 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require("express");
const http = require("http");
const path = require("path");
const socketIo = require("socket.io");
const mongoose = require("mongoose");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
// Page routes.
let routes = require("./src/routes");
let socketHandle = require("./src/socket");
const authApiRoute = require("./src/apiRoutes/auth");
const gameApiRoute = require("./src/apiRoutes/game");
const authenticate = require("./src/middleware/authenticate");
// Database connection using Heroku or localhost
let mongoDBUrl =
process.env.MONGODB_URI || "mongodb://localhost:27017/connect4";
mongoose.connect(mongoDBUrl, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true,
});
// Hold a reference to the connection.
const db = mongoose.connection;
db.on("error", (err) => {
console.log("A connection error occurred in server.js");
console.log(err);
});
db.once("open", async () => {
console.log("Connected to database.");
// Set mongoose debugging information to show in console
mongoose.set("debug", true);
});
// Initialise the app
let app = express();
let server = http.createServer(app);
// Set up websocket.
let io = socketIo(server);
// Configure app
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
// Configure to use statics
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "node_modules")));
// Use ejs
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/src/views"));
// Define page routes.
app.get("/", authenticate, routes.homeRoute);
app.get("/game/:gameId", authenticate, routes.gameRoute);
app.get("/login", routes.loginRoute);
app.get("/register", routes.registerRoute);
app.get("/video", routes.videoRoute);
// Define API routes.
app.use("/api", authApiRoute);
app.use("/api", gameApiRoute);
// Handle websocket connections.
io.on("connection", socketHandle.handleConnection);
// Cleanup functions for running locally
const cleanUp = (eventType) => {
stop();
};
[
`exit`,
`SIGINT`,
`SIGUSR1`,
`SIGUSR2`,
`uncaughtException`,
`SIGTERM`,
].forEach((eventType) => {
process.on(eventType, cleanUp.bind(null, eventType));
});
function stop() {
mongoose.connection.close(() => {
console.info("closed");
server.close();
process.exit();
});
}
module.exports = {
app: server,
stop: stop,
};