-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (58 loc) · 1.77 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
import express from "express";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import cors from "cors";
const { json } = bodyParser;
// Import routes
import profileRoutes from "./routes/profile.js";
import connectRoutes from "./routes/connect.js";
import feedRoutes from "./routes/feed.js";
import discoverRoutes from "./routes/discover.js";
import authRoutes from "./routes/auth.js";
const port = process.env.PORT || 8080;
const app = express();
app
// .use(cors)
.use(json())
.use((req, res, next) => {
// Set up CORS
res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS");
res.header("Access-Control-Allow-Methods", "*");
res.header(
"Access-Control-Allow-Headers",
// "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Origin, Access-Control-Allow-Methods,Access-Control-Allow-Headers"
"*"
);
next();
})
.use("/profile", profileRoutes)
.use("/connect", connectRoutes)
.use("/feed", feedRoutes)
.use("/discover", discoverRoutes)
.use("/auth", authRoutes)
.use((error, req, res, next) => {
// Error handling
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res
.status(status)
.json({ message: message, data: data, statusCode: status });
});
// Set up DB Connection
import db from "./config/db.config.js";
mongoose
.connect(db.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(port, () => {
console.log(`DB Connected and server running on ${port}`);
});
})
.catch((err) => {
console.log("Cannot connect to the database!", err);
process.exit();
});