-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (65 loc) · 1.95 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const cors = require("cors");
const bodyParser = require("body-parser");
const passport = require("passport");
const LocalStrategy = require("passport-local");
require("dotenv").config();
const User = require("./models/user");
const path = require("path");
//Connect to Mongo
mongoose
.connect(process.env.DB_CONNECT, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
console.log("MongoDB Connected");
})
.catch((err) => {
console.log(err);
});
//Middleware
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Passport config
app.use(
require("express-session")({
secret: "I Love Husky",
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60 * 60 * 1000 },
})
);
app.use(passport.initialize());
app.use(passport.session());
// passport.use(new LocalStrategy(User.authenticate()));
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
next();
});
//Routes
app.use("/", require("./routes/index"));
app.use("/tickets", require("./routes/tickets"));
app.use("/add", require("./routes/add"));
app.use("/explore", require("./routes/explore"));
app.use("/journals", require("./routes/journals"));
app.use("/personalCenter", require("./routes/personalCenter"));
app.use("/topic", require("./routes/topic"));
// 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"));
});
}
//Serve listen
var PORT = process.env.PORT || 4000;
app.listen(PORT, console.log(`Server started on port ${PORT}`));