2022-04-05 01:15:43 +00:00
|
|
|
import express from "express";
|
|
|
|
import bodyParser from "body-parser";
|
|
|
|
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";
|
|
|
|
import cors from "cors";
|
2022-04-05 16:54:06 +00:00
|
|
|
import rentalController from "./controllers/rentalController.js";
|
2022-04-05 01:15:43 +00:00
|
|
|
|
|
|
|
const server = express();
|
|
|
|
const port = process.env.PORT || 5000;
|
|
|
|
|
|
|
|
server.use(express.static("public")); // For all client files.
|
|
|
|
|
|
|
|
// Connection documentation: https://mongoosejs.com/docs/connections.html
|
|
|
|
try {
|
|
|
|
mongoose.connect(mongoURI, {
|
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
dbName: mongooseDbName,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
2022-04-05 22:24:41 +00:00
|
|
|
console.log("We are running in development mode.");
|
2022-04-05 01:15:43 +00:00
|
|
|
mongoose.set("bufferCommands", false); // We want to know if there are connection issues immediately for development. Disables globally.
|
2022-04-05 22:24:41 +00:00
|
|
|
server.use(cors({ credentials: true, origin: "http://localhost:3000" }));
|
2022-04-05 01:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Docs: https://www.npmjs.com/package/body-parser
|
|
|
|
server.use(bodyParser.json());
|
|
|
|
server.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
|
|
|
|
server.use(userSession);
|
|
|
|
|
2022-04-05 22:24:41 +00:00
|
|
|
server.use("/api/user", UserController);
|
|
|
|
server.use("/api/match", MatchController);
|
|
|
|
server.use("/api/sport", SportController);
|
|
|
|
server.use("/api/rental", rentalController);
|
2022-04-05 01:15:43 +00:00
|
|
|
|
|
|
|
server.listen(port, () => {
|
|
|
|
console.log(`Server listening on port ${port}.`);
|
|
|
|
});
|