-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
49 lines (41 loc) · 1.44 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
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const photosRoutes = require("./routes/api/photos");
const authRoutes = require("./routes/api/auth");
const userRoutes = require("./routes/api/user");
const commentRoutes = require("./routes/api/comments");
//use bodyparser
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.json({ limit: "50mb", extended: true }));
//use cors
app.use(cors());
//connect to MongoDB
mongoose
.connect(process.env.mongoDBURLDEV, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log(err));
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/user", userRoutes);
app.use("/api/v1/photos", photosRoutes);
app.use("/api/v1/photos/:id/comments", commentRoutes);
//serve static assets if we are in production
if (process.env.NODE_ENV === "production") {
//set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is starting at PORT ${PORT}`);
});