-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (40 loc) · 1.32 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
50
const express = require("express");
const path = require("path");
const cors = require("cors");
require("dotenv/config");
const app = express();
const PORT = process.env.PORT || 5000;
// MIDDLEWARE
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// IMPORT ROUTES
const userRoutes = require("./routes/api/users");
const authRoutes = require("./routes/api/auth");
const saltRoutes = require("./routes/api/salts");
const postRoutes = require("./routes/api/posts");
// USE ROUTES
app.use("/api/users", userRoutes);
app.use("/api/auth", authRoutes);
app.use("/api/salts", saltRoutes);
app.use("/api/posts", postRoutes);
// CONNECT TO DATABASE
const db = require("./database/database");
require("./database/associations")();
db.sync();
// db.sync({ force: true });
// TESTING DATABASE CONNECTION
db.authenticate()
.then(() => console.log("Connection to database successfully established"))
.catch((err) => console.log("Unable to connect to the database: ", err));
// SERVE STATIC ASSETS IF IN PRODUCTION
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"));
});
}
// START SERVER
app.listen(PORT, () => {
console.log("Server listening on port", PORT);
});