-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
36 lines (30 loc) · 925 Bytes
/
app.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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config();
// Use Body Parser for user input
app.use(express.json());
// Connect to MongoDB
mongoose
.connect(process.env.MongoDB_URI, {
useFindAndModify: true,
useUnifiedTopology: true,
useNewUrlParser: true
})
.then(() => console.log("Connected to MongoDB"))
.catch(err => console.log(err));
// Use Routes for todos
app.use("/api/todos", require("./routes/todos"));
// Serve static assets if 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"));
});
}
// Setup PORT = Localhost
app.listen(process.env.PORT, () =>
console.log(`App started on port ${process.env.PORT}`)
);