forked from dsckiet/uddeshhya_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (83 loc) · 2.36 KB
/
index.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
const express = require("express");
const bodyParser = require("body-parser");
const compression = require("compression");
const helmet = require("helmet");
const { NODE_ENV, PORT } = require("./config/index");
const { notFound, sendErrors } = require("./config/errorHandler");
const { logRequestMiddleware } = require("./middleware/log");
const app = express();
const cors = require("cors");
require("dotenv").config();
require("./config/dbconnection");
app.use(logRequestMiddleware);
app.use(compression());
app.use(helmet());
app.use(
cors({
exposedHeaders: "x-auth-token"
})
);
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);
app.use(
bodyParser.json({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);
if (NODE_ENV === "production") {
console.log = console.warn = console.error = () => {};
}
// load schemas
const User = require("./models/User");
const Volunteer = require("./models/Volunteer");
const Project = require("./models/Project");
const Team = require("./models/Team");
const BloodDonor = require("./models/BloodDonor");
const bloodRequest = require("./models/BloodRequest");
const Message = require("./models/Message");
const Donation = require("./models/Donation");
const Log = require("./models/Log");
// load routes
app.use("/api/v1", require("./routes/api/v1/index"));
app.use("/api/v1/admin", require("./routes/api/v1/admin"));
app.use("/api/v1/users", require("./routes/api/v1/users"));
app.use("/api/v1/projects", require("./routes/api/v1/projects"));
app.use("/api/v1/team", require("./routes/api/v1/team"));
app.use("/api/v1/bloodPortal", require("./routes/api/v1/bloodPortal"));
app.use("/api/v1/donate", require("./routes/api/v1/donate"));
// 404 route
app.use("*", notFound);
//Error Handlers
app.use(sendErrors);
// Allowing headers
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Credentials", true);
res.header(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS"
);
next();
});
//Setting up server
(async () => {
try {
await app.listen(PORT);
console.info(
`NODE_ENV: ${NODE_ENV}\nServer is up and running on Port ${PORT}`
);
} catch (err) {
console.info("Error in running server.", err);
}
})();