-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (51 loc) · 1.7 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
"use strict";
const express = require("express");
const path = require("path");
const helmet = require("helmet");
const cors = require("cors");
const bodyParser = require("body-parser");
const compression = require("compression");
const { whitelist } = require("./constants/whitelist");
const { sanitiseInput } = require("./utils/sanitise");
const { logger } = require("./logger");
const { router: authRouter } = require("./routes/authentication/router");
const { router: sampleRouter } = require("./routes/sample/router");
const { router: examRouter } = require("./routes/exam/router");
const passport = require("./config/passportConfig");
// const { addUserActivity } = require("./helper/addUserActivity");
const app = express();
const corsOptions = {
origin: function (origin, callback) {
// Allow Rest API Clients to be used for testing
if (
whitelist.indexOf(origin) !== -1 ||
process.env.NODE_ENV !== "production"
) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};
app.use("/upload", express.static(path.join(__dirname, "upload")));
app.use(cors(corsOptions));
app.use(passport.initialize());
app.use(helmet());
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("*", (req, _res, next) => {
req.body = sanitiseInput(req.body);
next();
});
app.use("/auth", authRouter);
app.use("/api", passport.authenticate("jwt", { session: false }));
app.use("/api/jwt", sampleRouter);
app.use("/api/exam", examRouter);
app.use(function (err, _req, res, _next) {
logger.error(err);
res.status(500).send({ success: false, error: "Oops: Something broke!" });
});
module.exports = {
app,
};