-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
41 lines (32 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
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
const morgan = require("morgan");
const initDb = require("./config/initDb");
const authRouter = require("./routes/auth");
const usersRouter = require("./routes/users");
const resultsRouter = require("./routes/results");
const continentRouter = require("./routes/continent");
const errorMiddleware = require("./routes/errorMiddleware");
const PORT = process.env.PORT || 3001;
// log all requests to the console in development
if (process.env.NODE_ENV !== "production") {
app.use(morgan("dev"));
}
// Setting up express to use json and set it to req.body
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
initDb();
// Serve up static assets in production (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
app.use(authRouter, usersRouter,resultsRouter, continentRouter, errorMiddleware);
// Send all other requests to react app
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, () => {
console.log(`🌎 ==> Server now on port ${PORT}!`);
});