-
Notifications
You must be signed in to change notification settings - Fork 31
/
app.js
101 lines (83 loc) · 2.66 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import express from "express";
const app = express();
import cookieParser from 'cookie-parser';
import passport from 'passport';
import connectDB from './config/database.js';
import session from 'express-session';
import MongoStore from 'connect-mongo';
import 'dotenv/config';
import passportConfig from "./config/passport.js";
passportConfig(passport);
import mockUser from './config/mockUser.json' assert { type: 'json' }
import User from './models/userModel.js'
// *****************************
// Sessions (MongoDB)
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
if (process.env.NODE_ENV === 'development') {
app.use(
session({
secret: process.env.SESSION_SECRET || 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DB_STRING, dbName: 'banki-brunch' })
})
)
} else {
app.use(
session({
secret: process.env.SESSION_SECRET || 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DB_STRING, dbName: 'banki-brunch' }),
cookie: {
secure: true, // Set to true if you're using HTTPS
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24, // 1 day
sameSite: "none",
}
})
)
}
app.use(passport.initialize());
app.use(passport.session());
// Set up mock user in development environment
if (process.env.NODE_ENV === "development") {
console.log("Running development environment (using Mock User)")
app.use(async (req, res, next) => {
if (process.env.MOCK_USER !== "true") return next()
req.user = mockUser
await User.findOneAndUpdate(
{ _id: mockUser._id },
{ $setOnInsert: mockUser },
{ upsert: true, new: true }
).exec()
next()
})
}
//import mongoose from "mongoose"; // not set up yet
// Import routers
import userRoutes from "./routes/userRoutes.js";
import authRoutes from "./routes/authRoutes.js";
// Import global middlewares
import errorHandler from "./middleware/errorHandler.js";
import logger from "./middleware/logger.js";
// Connect to MongoDB
// mongoose
// .connect("mongodb://localhost/bankiBrunch", {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// })
// .then(() => {
// console.log("Connected to MongoDB");
// });
app.use(logger); // Log every request
// Using the routers
app.use("/user", userRoutes);
app.use("/auth", authRoutes);
// Global error handler (should be the last middleware)
app.use(errorHandler);
const PORT = 3000;
connectDB().then(() => app.listen(PORT, () => console.log(`Server started on http://localhost:${PORT}`)))
export default app;