Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (56 loc) · 1.83 KB
/
Copy pathapp.js
File metadata and controls
71 lines (56 loc) · 1.83 KB
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
import express from "express";
import session from "express-session";
import connectMongo from "connect-mongo";
import passport from "passport";
import LocalStrategy from "passport-local";
import methodOverride from "method-override";
import flash from "connect-flash";
import { dirname } from "path";
import { fileURLToPath } from "url";
import User from "./models/user.model.js";
import indexRoutes from "./routes/index.routes.js";
import shoeRoutes from "./routes/shoe.routes.js";
import commentRoutes from "./routes/comment.routes.js";
import connectDB from "./config/db.js";
connectDB();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());
const MongoStore = connectMongo(session);
const store = new MongoStore({
url: process.env.DATABASEURL || "mongodb://localhost:27017/sneakerplace",
secret: "Something random lol",
touchAfter: 24 * 60 * 60,
});
app.use(
session({
secret: "Something random lol",
resave: false,
saveUninitialized: false,
store,
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use("/", indexRoutes);
app.use("/shoes", shoeRoutes);
app.use("/shoes/:id/comments", commentRoutes);
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`App listening on http://localhost:${port}`);
});