forked from rheannemr/Video-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (47 loc) · 1.53 KB
/
server.js
File metadata and controls
59 lines (47 loc) · 1.53 KB
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
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
const morgan = require("morgan");
const notesApiRoute = require("./routes/notes");
const { join } = require("path")
const cors = require("cors");
const PORT = process.env.PORT || 3001;
const app = express();
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(morgan("tiny"));
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
};
// Serve static assets from the /public folder
app.use(express.static(join(__dirname, "public")));
// Endpoint to serve the configuration file
app.get("/auth_config.json", (req, res) => {
res.sendFile(join(__dirname, "auth_config.json"));
});
// Mongo Connection
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/video-notes", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
})
app.use(
cors({
origin: "http://localhost:3000", // <-- location of the react app were connecting to
credentials: true,
})
);
// Define API route
app.use("/api/notes/", notesApiRoute)
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/public/index.html"));
});
const db = mongoose.connection
db.on("error", (error) => console.log(error));
db.once("open", () => console.log("Connected to Database"));
app.listen(PORT, () => {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});