-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (36 loc) · 1.19 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const apiRoute = require("./routes/api/main");
const userRoute = require('./routes/api/users')
const cors = require("cors");
const path = require("path");
// const config = require("./config/config");
require("dotenv").config();
// Set up port
const port = process.env.PORT || 5000;
const app = express();
// Enable cors
app.use(cors());
// Set BodyParser middleware
app.use(bodyParser.json());
// DB conection
// const dbURL = config.mongoDBUrl;
const dbURL = process.env.MONGODB_URL;
mongoose
.connect(dbURL, { useNewUrlParser: true })
.then(() => console.log("MongoDB connected..."))
.catch(err => console.error(err));
app.use(express.static(path.join(__dirname, "client", "build")));
// Use routes
app.use("/api/users", userRoute);
app.use("/", apiRoute);
// Serve the static assets if in the prodcution
if (process.env.NODE_ENV === "production") {
// Set static folder
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
}
// Start listening
app.listen(port, () => console.log("Server started at port " + port));