-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (39 loc) · 1.06 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
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const routes = require("./routes");
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 3001;
// Express middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(routes);
// Serve static assets for deployment
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// Connect to MongoDB
const db = process.env.MONGODB_URI;
const connect = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
console.log("MongoDB Connected...");
} catch (error) {
console.error(error.message);
process.exit();
}
};
connect();
// Start API server
app.listen(PORT, () => {
console.log(`API server now listening on PORT ${PORT}`);
});