-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (78 loc) · 1.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const passport = require("passport");
require("dotenv").config();
//Setup Passport
require("./passport")(passport);
//Setup App
const app = express();
app.use(
cors({
credentials: true,
origin: [process.env.FRONTEND_URL],
})
);
app.use(express.json());
//Session
app.use(
session({
secret: process.env.SESSION_SECRET || "keyboard cat",
resave: true,
saveUninitialized: false,
rolling: true,
cookie: {
sameSite: "lax",
httpOnly: true,
maxAge: 1000 * 60 * 15,
},
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);
//Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
const port = process.env.PORT || 8000;
//Connect to DB
mongoose.connect(
process.env.DB_CONNECTION,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
if (err) {
console.log(`Failed to Connect to MongoB. Error: ${err}`);
} else {
console.log("Connectd to MongoDB");
}
}
);
//Import Routes
const userRoute = require("./routes/user");
const itemsRoute = require("./routes/repeatingItems");
const intervalsRoute = require("./routes/repeatingIntervals");
const authRoute = require("./routes/auth");
//Route Middleware
app.use("/repeatingitems", itemsRoute);
app.use("/repeatingintervals", intervalsRoute);
app.use("/auth", authRoute);
app.use("/user", userRoute);
//Error handler middleware
app.use((err, req, res, next) => {
res.status(err.status || 500).json(err.message);
});
app.get("/", (req, res) => {
res.send("You are in the homepage 1 !!");
});
app.get("/login", (req, res) => {
res.send(req.sessionID);
});
app.get("/loggedout", (req, res) => {
res.send("You are logged out :(");
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});