forked from bekiTil/Temaribet-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (69 loc) · 2.19 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
require("dotenv").config();
const http = require("https");
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const log = require("./lib/chalkLog");
const app = express();
const port = process.env.PORT || 4000;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
// route imports
const userRoutes = require("./routes/user");
const tutorRoutes = require("./routes/tutor");
const parentRoutes = require("./routes/parent");
const studentRoutes = require("./routes/student");
const reportRoutes = require("./routes/report");
const jobRoutes = require("./routes/job");
const adminRoutes = require("./routes/admin");
const renewAccessTokenRoute = require("./routes/renewAccessToken");
// prisma instance
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
// route middlewares
app.use("/api/v1/user", userRoutes);
app.use("/api/v1/tutor", tutorRoutes);
app.use("/api/v1/parent", parentRoutes);
app.use("/api/v1/student", studentRoutes);
app.use("/api/v1/report", reportRoutes);
app.use("/api/v1/job", jobRoutes);
app.use("/api/v1/admin", adminRoutes);
app.use("/api/v1/refreshTokens", renewAccessTokenRoute);
// catching error
app.use((req, res, next) => {
const error = new Error("Not Found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
// keep server alive
const minInterval = 5 * 60 * 1000; // 5 minutes in milliseconds
const maxInterval = 15 * 60 * 1000; // 15 minutes in milliseconds
function getRandomInterval() {
return Math.floor(
Math.random() * (maxInterval - minInterval + 1) + minInterval
);
}
function keepServerAlive() {
let count = 0;
console.log("Calling server to keep it alive...");
http.get("https://temaribet-api.onrender.com");
count++;
const interval = getRandomInterval();
setTimeout(keepServerAlive, interval);
}
// listen to server
app.listen(port, () => {
log.success(`Server is running on port ${port}.`);
keepServerAlive();
});