-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
161 lines (142 loc) · 4.15 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//Imports
const express = require("express");
const expressLayouts = require("express-ejs-layouts");
var app = express();
const hbs = require("hbs");
let alert = require("alert");
app.use(express.json());
const path = require("path");
const bodyParser = require("body-parser");
const { registerHelper } = require("hbs");
const port = 3000;
const router = express.Router();
const mongoose = require("mongoose");
const staticPath = path.join(__dirname, "/public");
const templatePath = path.join(__dirname, "/templates/views");
const partialsPath = path.join(__dirname, "/templates/partials");
// require("./connection/connectdb");
const Register = require("./model/registers");
const Contactus = require("./model/contacts");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.set("view engine", "hbs");
app.set("views", templatePath);
app.use(express.static(staticPath));
hbs.registerPartials(partialsPath);
let urlencoded = bodyParser.urlencoded({ extended: false });
// Static Files
app.use(express.static("public"));
app.use(express.json());
app.use("/css", express.static(__dirname + "public/css"));
app.use("/js", express.static(__dirname + "public/js"));
app.use("/img", express.static(__dirname + "public/img"));
//Set Views
let connectdb = require("./connection/connectdb.js");
connectdb();
app.get("/", (req, res) => {
res.render("index");
});
app.get("/about", (req, res) => {
res.render("about");
});
app.get("/tour", (req, res) => {
res.render("index");
});
app.get("/adventure", (req, res) => {
res.render("adventure");
});
app.get("/uk", (req, res) => {
res.render("uk");
});
app.get("/contact", (req, res) => {
res.render("contact");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/register", (req, res) => {
res.render("register");
});
app.get("*", (req, res) => {
res.render("404");
});
app.post("/register", async (req, res) => {
try {
const password = req.body.Password;
const cpassword = req.body.ConfirmPassword;
const email = req.body.Email;
function isEmail(email) {
var emailFormat = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
if (email !== "" && email.match(emailFormat)) {
return true;
}
return false;
}
if (isEmail(email) && password === cpassword) {
const data = new Register({
Email: req.body.Email,
Username: req.body.Username,
Phone: req.body.Phone,
Password: req.body.Password,
ConfirmPassword: req.body.ConfirmPassword,
});
const registered = await data.save();
res.status(201).render("login");
console.log(registered);
alert("Successfully Registered");
} else {
alert("Invalid Username or Password");
res.render("register");
}
} catch (error) {
res.status(400).send(error);
}
});
//contact
app.post("/contact", async (req, res) => {
try {
const email = req.body.email;
function isEmail(email) {
var emailFormat = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
if (email !== "" && email.match(emailFormat)) {
return true;
}
return false;
}
if (isEmail(email)) {
const contactdata = new Contactus({
name: req.body.name,
email: req.body.email,
message: req.body.message,
});
const registered2 = await contactdata.save();
res.status(201).render("index");
console.log(registered2);
alert("Your message has been sent successfully!");
} else {
alert("Invalid Credentials");
res.render("contact");
}
} catch (error) {
res.status(400).send(error);
}
});
//login check
app.post("/login", async (req, res) => {
try {
const email = req.body.Email;
const password = req.body.Password;
const useremail = await Register.findOne({ Email: email });
if (useremail.Password === password) {
res.status(201).render("index");
alert("Successfully Login");
} else {
alert("Invalid Credentials");
res.render("login");
}
console.log(useremail);
} catch (error) {
res.status(400).send("Invalid Email");
}
});
app.listen(port, () => console.info(`Listening on port ${port}`));