-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
66 lines (48 loc) · 1.71 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
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const cors = require("cors");
const globalErrorHandler = require("./Controller/errorController");
const authRouter = require("./Routes/authRoute");
const auditorRouter = require("./Routes/auditorRoute");
const contestRouter = require("./Routes/contestRoute");
const judgeRouter = require("./Routes/judgeRoutes");
const AppError = require("./utils/appError");
dotenv.config();
const app = express();
const allowList = [process.env.ALLOWED_URL_1, process.env.ALLOWED_URL_2];
var corsOptionsDelegate = function (req, callback) {
var corsOptions = {
credentials: true,
};
if (allowList.indexOf(req.header("Origin")) !== -1) {
corsOptions.origin = true; // reflect (enable) the requested origin in the CORS response
} else {
corsOptions.origin = false; // disable CORS for this request
}
callback(null, corsOptions); // callback expects two parameters: error and options
};
app.use(express.json());
app.use(cors(corsOptionsDelegate));
console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use("/test", (req, res) => {
res.send("Working");
});
app.use(express.json({ limit: "8mb" }));
//All the routes comes here
app.get("/api/v1", (req, res, next) => {
res.send("Test working");
});
app.use("/solsec/auth",authRouter);
app.use("/solsec/auditor", auditorRouter);
app.use("/solsec/contest", contestRouter);
app.use("/solsec/judge", judgeRouter);
app.use("*", (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
//This is the global error handler
app.use(globalErrorHandler);
module.exports = app;