-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (71 loc) · 2.22 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
require("./database/connection");
const express = require("express");
const app = express();
const usersRoutes = require("./routes/user");
const therapistsRouter = require("./routes/therapists");
const cookieParser = require("cookie-parser");
const sessions = require("express-session");
const { waitList } = require("./database/models");
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(express.json());
app.use(
sessions({
secret: "donttellanyone",
saveUninitialized: true,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
resave: false,
})
);
app.use(cookieParser());
// parse incoming post request's body
app.use(express.urlencoded({ extended: false }));
// app.use("/user", usersRoutes);
// app.use("/th", therapistsRouter);
// app.get("/", function (req, res) {
// res.render("pages/index");
// });
// app.get("/design", (req, res) => {
// res.render("pages/design");
// });
// //render the landing pages
// app.get("/about", (req, res) => {
// res.render("pages/landing-page/about-us")
// })
// app.get("/blog", (req, res) => {
// res.render("pages/landing-page/blog-post")
// })
app.get("/list", (req, res) => {
res.render("pages/waiting-list")
})
app.post("/list", async (req, res) => {
let { email } = req.body;
email = email.toLowerCase();
if (email) {
const emailExists = await waitList.findOne({ email });
if (emailExists) {
//frontend needs to display the error somewhere
//the formdata is for filling back the input fields(page reloads after submission)
res.render("pages/waiting-list", {error: "email already exists"});
} else {
const newEmail = new waitList({ email });
newEmail.save((err) => {
if (err) {
//server error
//frontend needs to display the error somewhere
//the formdata is for filling back the input fields(page reloads after submission)
res.redirect("pages/waiting-list", {
error: "something went wrong please try again",
});
} else {
console.log("success");
}
});
}
}
})
app.use((req, res) => {
res.render("templates/404");
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Listening on ${PORT}`));