-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
43 lines (37 loc) · 1.21 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
// Required Consts!
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const db = require("./models");
require("dotenv").config();
const cors = require("cors");
const session = require("express-session");
const path = require("path");
// Set up for Heroku or Port 5000 cause I'm crazy!
const PORT = process.env.PORT || 5000;
// Middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
// Optimize for Heroku
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// For Authenticaion
app.use(session({ secret: "secret" }));
// API Routes start with API and live at apiRoutes.js
app.use("/api", require("./routes/apiRoutes"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
// Connect to the mongo database!
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/project-3", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
// Let the user know the server is running, and which port. Yeay!
app.listen(PORT, () => {
console.log(`listening at http://localhost:${PORT}`);
});