forked from Ticklers/Battle-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·101 lines (81 loc) · 2.47 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const passport = require("passport");
const multer = require("multer");
const path = require("path");
var cors = require("cors");
// const upload = multer({dest: 'uploads/'});
mongoose.Promise = global.Promise;
const app = express();
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header(
"Access-Control-Allow-Methods",
"PUT, POST, PATCH, DELETE, GET, OPTIONS"
);
return res.status(200).json({});
}
next();
});
// app.use(
// cors({
// allowedHeaders: ["sessionId", "Content-Type"],
// exposedHeaders: ["sessionId"],
// origin: "*",
// methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
// preflightContinue: false,
// })
// );
app.get("/", (req, res) => res.send("Root path of the app"));
const db = require("./config/keys").mongoURI;
mongoose
.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => console.log("MongoDB Connected Succesfully"))
.catch((err) => console.log(err));
// app.use('/uploads', express.static('uploads'));
const users = require("./routes/users");
app.use("/api/users", users);
const userUpdates = require("./routes/userUpdate");
app.use("/api/userupdate", userUpdates);
const memes = require("./routes/memes");
app.use("/api/memes", memes);
const addons = require("./routes/memeAddons");
app.use("/api/addons", addons);
const battles = require("./routes/battles");
app.use("/api/battles", battles);
const feed = require("./routes/feed");
app.use("/api/feed", feed);
app.get("/search", function (req, res) {
res.sendFile(path.join(__dirname + "/header.html"));
});
app.use(passport.initialize());
require("./config/passport")(passport);
app.use((req, res, next) => {
const error = new Error("Route not found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log("server is running on port: " + port));