-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
97 lines (79 loc) · 2.85 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
//package imports
import "express-async-errors";
import express from "express";
import morgan from "morgan";
import mongoose from "mongoose";
import * as dotenv from "dotenv";
import cloudinary from "cloudinary";
import helmet from "helmet";
import mongoSanitize from "express-mongo-sanitize";
import { makeRequest } from "./utils/avoidSpinDown.js";
import dayjs from "dayjs";
dotenv.config();
const app = express();
const port = process.env.PORT;
import cookieParser from "cookie-parser";
//public
import { dirname } from "path";
import { fileURLToPath } from "url";
import path from "path";
//router imports
import jobRouter from "./routes/jobRouter.js";
import authRouter from "./routes/authRouter.js";
import userRouter from "./routes/userRouter.js";
//middleware imports
import errorHandlerMiddleware from "./middleware/errorHandlerMiddleware.js";
import { authenticateUser } from "./middleware/authMiddleware.js";
import { log } from "console";
//cloudinary API
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET,
});
const DATABASE_URI =
process.env.DATABASE_URI || "mongodb://127.0.0.1:27017/jobifyDB";
//connect to mongo database
try {
await mongoose.connect(DATABASE_URI);
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
} catch (error) {
console.log(error);
process.exit(1);
}
//path to public folder, temp storage for avatar uploads
const __dirname = dirname(fileURLToPath(import.meta.url));
app.use(express.static(path.resolve(__dirname, "./client/dist")));
app.use(express.json());
app.use(cookieParser());
app.use(helmet());
app.use(mongoSanitize());
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
///creating starting url for controllers/routes
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/users", authenticateUser, userRouter);
app.use("/api/v1/jobs", authenticateUser, jobRouter);
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "./client/dist", "index.html"));
});
//not found middleware - handles requests for non-existent routes
app.use("*", (req, res) => {
res.status(404).json({ msg: "requested resource not found" });
});
//error middleware - catch-all for handling unexpected errors that occur during request processing
//express-async package handles async errors and passes them to this middleware without needing try-catch block
app.use(errorHandlerMiddleware);
// Call request function every 14.5 minutes if local time is b/w 8am and 8pm on a weekday to avoid server spin down
const morning = 8;
const evening = 20;
const current_hour = dayjs().hour();
const current_day = dayjs().day();
const isWeekday = current_day >= 1 && current_day <= 5;
const upTime = current_hour > morning && current_hour < evening;
if (upTime && isWeekday) {
setInterval(makeRequest, 1000 * 62 * 14);
}