-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
169 lines (142 loc) · 4.17 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
166
167
168
169
const express = require("express");
const expressOasGenerator = require("express-oas-generator");
const cors = require("cors");
const path = require("path");
const passport = require("passport");
const fs = require("fs");
const mkdirp = require("mkdirp");
const axios = require("axios");
// const socketio = require("socket.io");
/**
* -------------- GENERAL SETUP ----------------
*/
var whitelist = [
"http://localhost:3001",
"http://classwhisper.com",
"https://classwhisper.com",
"http://www.classwhisper.com",
"https://www.classwhisper.com",
];
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
};
require("dotenv").config();
var app = express();
// Handle the responses
// if (process.env.NODE_ENV !== "production") {
// const openAPIFilePath = "./documentation/api.json";
// mkdirp.sync(path.parse(openAPIFilePath).dir);
// let predefinedSpec;
// try {
// predefinedSpec = JSON.parse(
// fs.readFileSync(openAPIFilePath, { encoding: "utf-8" })
// );
// } catch (e) {
// console.log(e);
// }
// expressOasGenerator.handleResponses(app, {
// specOutputPath: openAPIFilePath,
// writeIntervalMs: 0,
// predefinedSpec: predefinedSpec ? () => predefinedSpec : undefined,
// });
// }
// Configuring the database and opening a global connection
require("./config/database");
// loading the models
require("./models/user");
require("./models/Post");
require("./models/Class");
require("./models/ClassEnrollment");
require("./models/Comment");
require("./models/Notifications");
require("./models/Vote");
require("./models/Conversation");
require("./models/Message");
// Passing the global passport object into the configuration function
require("./config/passport")(passport);
// Initialize the passport object on every request
app.use(passport.initialize());
// Replace of body-parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.options("*", cors());
/**
* -------------- ROUTES ----------------
*/
// Imports all of the routes from ./routes/index.js
app.use(require("./routes"));
// OAS
// expressOasGenerator.handleRequests();
// Save all classes from roster for every subject into DB
const mongoose = require("mongoose");
const Class = mongoose.model("Class");
Class.countDocuments(async function (err, count) {
if (!err && count === 0) {
const subjectsResponse = await axios(
"https://classes.cornell.edu/api/2.0/config/subjects.json?roster=FA20"
);
const subjects = subjectsResponse.data.data.subjects;
for (const subject of subjects) {
console.log(subject);
const response = await axios(
`https://classes.cornell.edu/api/2.0/search/classes.json?roster=FA20&subject=${subject.value}`
);
const classes = response.data.data.classes.map(
({
titleShort,
catalogNbr,
crseId,
description,
subject,
titleLong,
}) => {
return {
catalogNbr: catalogNbr,
subject: subject,
titleShort: titleShort,
description: description,
term: "FA20",
crseId: crseId,
titleLong: titleLong,
completeTitle: `${subject} ${catalogNbr} ${titleLong}`,
};
}
);
await Class.create(classes);
}
}
}).then(() => {
console.log("done");
/**
* Serve react app
*/
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("/*", function (req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
}
/**
* -------------- SERVER ----------------
*/
const server = app.listen(process.env.PORT || 3000);
/**
* -------------- WEB SOCKET ----------------
*/
// const io = socketio.listen(server);
// io.sockets.on("connection", function (socket) {
// socket.on("join", (data) => {
// console.log(data);
// });
// socket.on("disconnect", () => {
// console.log("gone");
// });
// });
});