-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (110 loc) · 3.47 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
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express");
const config = require("./config/firebase.config");
const { initializeApp } = require("firebase/app");
const mongoose = require("mongoose");
const healthcheck = require("./routes/api");
const auth = require("./routes/auth");
const clas = require("./routes/class");
const timetable = require("./routes/timetable");
const buysell = require("./routes/buysell");
const bully = require("./routes/bully");
const helmet = require("helmet");
const { student, teacher } = require("./models/user");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const port = process.env.PORT || 5000;
const URI = process.env.URI;
const connectWithRetry = (uris, options, maxAttempts = 5) => {
connectWithRetry.timeout = connectWithRetry.timeout || 0;
return mongoose.connect(uris, options, (err) => {
if (err)
if (connectWithRetry.timeout <= (maxAttempts - 1) * 5000) {
console.error(
`Failed to connect to mongo on startup - retrying in ${
(connectWithRetry.timeout += 5000) / 1000
} sec`,
connectWithRetry.previousError != "" + err
? `\n${(connectWithRetry.previousError = err)}`
: ""
);
setTimeout(connectWithRetry, connectWithRetry.timeout, uris, options);
} else process.exit(1);
else console.log("Connected to MongoDB successfully!");
});
};
connectWithRetry(URI, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
initializeApp(config.firebaseConfig);
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
app.use(helmet());
app.use(
cors({
allowedHeaders: [
"Content-Type",
"token",
"authorization",
"*",
"Content-Length",
"X-Requested-With",
],
origin: "*",
preflightContinue: true,
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// var students = [{
// role:"student",
// email: "student@gmail.com",
// profileName:"demo",
// rollno: "BT19CSE000",
// batch:"2019-2023",
// branch: "CSE",
// profilePhotoUri:"",
// section: "A",
// password:"qwerty123#",
// e_card: "https://firebasestorage.googleapis.com/v0/b/our-e-college-app-909e3.appspot.com/o/Batch%2F2019-2023%2FBranch%2FCSE%2FStudents%2FBT19CSE005%2FE-Card%2Fchota.PNG?alt=media&token=19d84d07-f707-48f9-9ccc-e9a768a9dfbd"}];
// students.forEach(function(item){
// student.create(item,function(err,algo){
// if(err){
// console.log(err);}
// else
// {console.log(algo);}
// });
// });
// var teachers = [{
// role:"teacher",
// email: "teacher@gmail.com",
// password:"qwerty123#",
// profileName:"teacher",
// profilePhotoUri:""}];
// teachers.forEach(function(item){
// student.create(item,function(err,algo){
// if(err){
// console.log(err);}
// else
// {console.log(algo);}
// });
// });
app.use("/api", healthcheck);
app.use("/api", auth);
app.use("/api", clas);
app.use("/api", buysell);
app.use("/api", timetable);
app.use("/api", bully);
app.use((err, req, res, next) => {
console.error(err);
res.status(422).send({ success: false, error: err.message });
});
const server = require("http").createServer(app);
server.listen(port, () => {
console.log("Server has started! on http://localhost:" + port + "/");
});