-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.mjs
More file actions
91 lines (67 loc) · 2.28 KB
/
app.mjs
File metadata and controls
91 lines (67 loc) · 2.28 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import express from "express";
import session from "express-session";
import MongoStore from "connect-mongo";
import mongoose from "mongoose";
import passport from "passport";
import path from 'path'
import dotenv from "dotenv"
import { fileURLToPath } from 'url';
import {User} from "./schemas/user.mjs";
import {Puzzle} from "./schemas/puzzle.mjs";
import {PlayRecord} from "./schemas/playRecord.mjs";
import {Liked, Favorites, Saved} from "./schemas/bookmark.mjs";
import connectDB from "./db.mjs";
import authRouter from "./routes/auth.mjs"
import createRouter from "./routes/create.mjs";
import playRouter from "./routes/puzzle.mjs";
dotenv.config();
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.set("view engine", "hbs");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(session({
secret: "dev-secret",
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DSN}),
}));
app.use(passport.authenticate('session'));
connectDB()
app.listen(process.env.PORT || 3000);
/* --- Login and Sign Up --- */
app.use("/", authRouter);
/* --- Create Puzzle --- */
app.use("/", createRouter);
/* --- Play Puzzle --- */
app.use("/", playRouter)
/* --- Main Page */
app.get("/", async (req, res) => {
const puzzles = await Promise.all (
( await Puzzle.find({}) )
.map( async (puzzle) => {
puzzle.authorName = ( await User.findOne({ _id: puzzle.author }) ).username ;
puzzle.webTitle = puzzle.title.replaceAll(" ", "-")
return puzzle;
})
);
if (req.user){
return res.render("index", {user: req.user, puzzles: puzzles});
}
return res.render("index", {puzzles: puzzles});
});
/* --- User and Bookmarks --- */
app.get("/user/:username", async (req, res) => {
return res.render("user", {});
});
app.get("/user/:username/saved", async (req, res) => {
return res.render("user-saved", {});
});
app.get("/user/:username/liked", async (req, res) => {
return res.render("user-liked", {});
});
app.get("/user/:username/favorites", async (req, res) => {
return res.render("user-favorites", {});
});