-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (90 loc) · 3.34 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
94
95
96
97
98
99
100
101
102
103
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const env = process.env.NODE_ENV || "development";
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
// dotenv
if (env === "development") {
require("dotenv").config();
}
// connect to Mongo when the app initializes
const mongoOpts = {
useNewUrlParser: true
};
let connectionString =
env === "development"
? "mongodb://localhost:27017/hatchet"
: `mongodb://${process.env.DB_USER}:${
process.env.DB_PASS
}@ds263408.mlab.com:63408/hatchet`;
mongoose.connect(
connectionString,
mongoOpts
);
var db = mongoose.connection || null;
//handle mongo error
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", function() {
// we're connected!
});
// Serve static files from the React app
app.use(express.static(path.join(__dirname, "client/build")));
const fightApi = require("./controllers/fight.js");
app.post("/api/:userId/fight", fightApi.newFight);
app.post("/api/:fightId/fight/setLive", fightApi.setLive);
app.post("/api/:fightId/vote", fightApi.vote);
app.post("/api/search", fightApi.getFightsAndUsersFromSearch);
app.patch("/api/:username/voteReason", fightApi.voteReason);
app.delete("/api/:userId/:fightId", fightApi.cancelFight);
app.patch("/api/:userId/:fightId", fightApi.surrender);
app.get("/api/fights", fightApi.getFights);
app.get("/api/:fightId/fight", fightApi.getFight);
app.get("/api/:userId/fights", fightApi.getUserFights);
app.get("/api/:userId/getWatchedFights", fightApi.getWatchedFights);
app.get("/api/fights/categories/:category", fightApi.getFightsByCategory);
app.get("/api/remind/:fightId", fightApi.remindUserOfFight);
const userApi = require("./controllers/user.js");
app.post("/api/join", userApi.join);
app.post("/api/login", cors(), userApi.login);
app.post("/api/resetPassword", userApi.resetPassword);
app.post("/api/resend", userApi.resendTokenPost);
app.post("/api/:userId/avatar", userApi.setAvatar);
app.patch("/api/:userId/:fightId/setWatch", userApi.setWatch);
app.get("/api/:userId/logout", userApi.logout);
app.get("/api/confirmation/:token_id", userApi.confirmationPost);
app.get("/api/:userId/avatar", userApi.getAvatar);
app.get("/api/:userName/avatar/username", userApi.getAvatarByUserName);
app.get("/api/:userId", userApi.getUser);
app.get("/api/:userId/record", userApi.getUserRecord);
app.get("/api/:userReference/isUser", userApi.isUser);
app.get(
"/api/:userId/:fightId/isUserWatchingFight",
userApi.isUserWatchingFight
);
app.get("/api/:userId/:fightId/hasUserVoted", userApi.hasUserVoted);
if (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"));
});
} else {
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/public/index.html"));
});
}
const port = process.env.PORT || 5000;
app.listen(port);