-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
165 lines (138 loc) · 4.05 KB
/
app.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require("express");
const passport = require("passport");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const helmet = require("helmet");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
require("dotenv").config();
const Club = require("./api/models/club.model");
const Student = require("./api/models/student.model");
const Test = require("./api/models/test.model");
const Question = require("./api/models/question.model");
const jwt = require("jsonwebtoken")
// process.env = secureEnv({ secret: "enimasinobhaniyo" });
const useragent = require("express-useragent");
const app = express();
app.set('trust proxy', 1);
var limiter = new rateLimit({
windowMs: 1 * 60 * 1000,
max: 60,
message:
"Too many requests created from this IP, please try again after an hour",
});
app.use(limiter)
const passport_config = require("./api/config/studentGoogleAuth");
const { errorLogger } = require("./api/utils/logger");
//Require Atlas database URI from environment variables
const DBURI = process.env.DBURI;
//Connect to MongoDB client using mongoose
mongoose
.connect(DBURI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log("Database Connected")
Club.init();
Student.init();
Test.init();
Question.init();
})
.catch((err) => {
errorLogger.error(`System: NIL >> ${err.toString()}`);
});
mongoose.Promise = global.Promise;
//Use helmet to prevent common security vulnerabilities
app.use(helmet());
//Set static folder
app.use("/uploads", express.static("./public"));
//Use body-parser to parse json body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// initialize passport
app.use(passport.initialize());
app.use(passport.session());
// Allow CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, auth-token"
);
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).json({});
}
next();
});
app.use(cors());
app.use(useragent.express());
if (process.env.NODE_ENV == "production") {
app.use((req, res, next) => {
if (req.useragent["isBot"] == false) {
next();
} else {
res.status(401).json({
message:
"Please try using a different browser: Interception is blocked",
});
}
});
}
app.use("/api/club", require("./api/routes/club.routes"));
app.use("/api/student", require("./api/routes/student.routes"));
app.use("/api/test", require("./api/routes/test.routes"));
app.use("/api/studentForm", require("./api/routes/student.form.routes"));
app.use("/api/easterEgg", require("./api/routes/easterEggForm.routes"));
app.use("/api/auth", require("./api/routes/auth.routes"));
app.get("/checkServer", (req, res) => {
return res.status(200).json({
message: "Server is up and running",
});
});
app.post("/admin", (req,res) => {
let {pass} = req.body;
if(pass==process.env.CHECKER){
const token = jwt.sign(
{
payload: process.env.JWT_PAYLOAD_SECRET
},
process.env.JWT_SECRET,
{
expiresIn: "1d",
}
);
res.status(200).send({
token
})
}
res.status(401).send({
"message":"Could Not Authenticate: Invalid Pass"
})
})
if (process.env.NODE_ENV == "development") {
app.use("/dev", require("./api/routes/dev.routes"));
}
//This function will give a 404 response if an undefined API endpoint is fired
app.use((req, res, next) => {
const error = new Error("Route not found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
const PORT = process.env.PORT || 3000;
//Start the server
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
module.exports = app;