-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
85 lines (74 loc) · 3.01 KB
/
server.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
const Sentry = require("@sentry/node");
const IS_DEBUG = true;
Sentry.init({
dsn: "https://[email protected]/1365884",
beforeSend: (event, hint) => {
if (IS_DEBUG) {
console.error(hint.originalException || hint.syntheticException);
return null; // this drops the event and nothing will be send to sentry
}
return event;
}
});
// import environmental variables from our variables.env file
require("dotenv").config({ path: ".env" });
/* Mongoose DB setup
–––––––––––––––––––––––––––––––––––––––––––––––––– */
const mongoose = require("mongoose");
// Connect to our Database and handle any bad connections
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on("error", err => {
console.error(`🙅 🚫 🙅 🚫 🙅 🚫 🙅 🚫 → ${err.message}`);
});
require("./models/Sorter");
require("./models/Algorithm");
require("./models/SortingResult");
require("./models/StudyAnalysisResult");
require("./models/StudySet");
require("./models/General");
require("./models/NewsPost");
/* Express Isomorphic
–––––––––––––––––––––––––––––––––––––––––––––––––– */
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
const port = process.env.PORT || 5000;
const apiroutes = require("./apiroutes");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
// Allow isomorphic requests
let clientUri = process.env.CLIENT_URI || "http://localhost:3000";
res.setHeader("Access-Control-Allow-Origin", clientUri);
// Request methods
res.setHeader("Access-Control-Allow-Methods", "GET, POST");
// Request headers
res.setHeader(
"Access-Control-Allow-Headers",
"X-Requested-With,content-type"
);
// Include cookies in the requests sent to the API (sessions)
res.setHeader("Access-Control-Allow-Credentials", true);
// onward
next();
});
/* API
–––––––––––––––––––––––––––––––––––––––––––––––––– */
// Handle all the api routing!
app.use("/", apiroutes);
/* Client Server
–––––––––––––––––––––––––––––––––––––––––––––––––– */
if (process.env.NODE_ENV === "production") {
// Serve any static files
app.use(express.static(path.join(__dirname, "client/build")));
// Handle React routing, return all requests to React app
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
app.listen(port, () => console.log(`🖥️ Server listening on port ${port}`));