-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (43 loc) · 1.46 KB
/
index.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
51
52
const express = require("express");
const cookieSession = require("cookie-session");
const passport = require("passport");
const authRoutes = require("./routes/auth");
const apiRoutes = require("./routes/api");
require("./passport");
const mongoose = require("mongoose");
const path = require("path");
const bodyParser = require("body-parser");
const cors = require("cors");
let config;
if (!process.env.IS_LIVE) {
config = require("./config/keys");
}
const app = express();
app.use(cors());
//MongoDB Setup
const mongodb = process.env.mongodburl || config.mongodburl;
mongoose.connect(mongodb, { useNewUrlParser: true, useUnifiedTopology: true });
// mongoose.set('debug', true); uncomment in order to see queries being made in console
//BodyParser Setup
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// set up session cookies
app.use(
cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: ["dsdsdsifi"],
})
);
// initialize passport
app.use(passport.initialize());
app.use(passport.session({ saveUninitialized: false, resave: false }));
app.use("/api", apiRoutes);
app.use("/auth", authRoutes);
/*Adds the react production build to serve react requests*/
app.use(express.static(path.join(__dirname, "client/build")));
/*React root*/
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/build/index.html"));
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`server on port ${port}`));