-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (60 loc) · 1.69 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
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const cors = require("cors");
require("dotenv/config");
const PORT = process.env.PORT || 5000;
const app = express();
// MIDDLEWARE
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// API ROUTES
const authRoutes = require("./routes/api/auth");
const slotDateRoutes = require("./routes/api/slotDate");
const postRoutes = require("./routes/api/post");
app.use("/api/auth", authRoutes);
app.use("/api/slotDates", slotDateRoutes);
app.use("/api/posts", postRoutes);
// CONNECT TO DB
// mongoose.connect(
// process.env.DB_CONNECTION,
// {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// useFindAndModify: false,
// useCreateIndex: true,
// },
// () => {
// console.log("Connected to DB!");
// // START SERVER
// app.listen(PORT, () => {
// console.log("Server now listening on port 5000");
// });
// }
// );
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.DB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
// SERVE STATIC ASSETS IF IN PRODUCTION
app.use(express.static("client/build"));
app.get("*", (_req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
//Connect to the database before listening
connectDB().then(() => {
app.listen(PORT, () => {
console.log("listening for requests");
});
});