2022-03-27 23:30:18 +00:00
|
|
|
import express from "express";
|
2022-04-01 10:36:10 +00:00
|
|
|
import bodyParser from "body-parser";
|
2022-04-01 15:27:58 +00:00
|
|
|
import mongoose from "mongoose";
|
|
|
|
import UserController from "./controllers/userController.js";
|
|
|
|
import MatchController from "./controllers/matchController.js";
|
|
|
|
import SportController from "./controllers/sportController.js";
|
|
|
|
import { userSession } from "./middleware/authority.js";
|
|
|
|
import { mongooseDbName, mongoURI } from "./database/mongoose.js";
|
2022-04-05 01:00:12 +00:00
|
|
|
import cors from "cors";
|
2022-03-27 23:30:18 +00:00
|
|
|
|
|
|
|
const server = express();
|
2022-04-01 15:27:58 +00:00
|
|
|
const port = process.env.PORT || 5000;
|
2022-03-27 23:30:18 +00:00
|
|
|
|
2022-04-01 10:36:10 +00:00
|
|
|
server.use(express.static("public")); // For all client files.
|
|
|
|
|
2022-04-01 15:27:58 +00:00
|
|
|
// Connection documentation: https://mongoosejs.com/docs/connections.html
|
|
|
|
try {
|
|
|
|
mongoose.connect(mongoURI, {
|
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
dbName: mongooseDbName,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:00:12 +00:00
|
|
|
|
2022-04-01 10:36:10 +00:00
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
mongoose.set("bufferCommands", false); // We want to know if there are connection issues immediately for development. Disables globally.
|
2022-04-05 01:00:12 +00:00
|
|
|
|
|
|
|
server.use(cors());
|
2022-04-01 10:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Docs: https://www.npmjs.com/package/body-parser
|
|
|
|
server.use(bodyParser.json());
|
|
|
|
server.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
|
|
|
|
server.use(userSession);
|
|
|
|
|
|
|
|
server.use("/user", UserController);
|
|
|
|
server.use("/match", MatchController);
|
|
|
|
server.use("/sport", SportController);
|
|
|
|
|
|
|
|
|
|
|
|
server.listen(port, () => {
|
|
|
|
console.log(`Server listening on port ${port}.`);
|
|
|
|
});
|