-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (102 loc) · 3.39 KB
/
index.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
const express = require("express");
const app = express();
const mysql = require("mysql2")
const ejsMate = require("ejs-mate");
app.engine('ejs', ejsMate);
const path = require("path");
const methodOverride = require("method-override");
app.use(methodOverride("_method"))
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'share_and_sustain',
password: 'Prince@102004@'
});
app.listen(8080, () => {
console.log("listning at port 8080");
})
app.get("/", (req, res) => {
res.render("index.ejs")
});
app.post("/newuser", (req, res) => {
let { username, email, address, pass1, pass2 } = req.body;
if (pass1 != pass2) {
res.render("wrongpassword.ejs");
} else {
q = `INSERT INTO users (username,email,address,password) VALUES ('${username}','${email}','${address}','${pass1}')`
try {
connection.query(q, (error, result) => {
if (error) res.send("some error in database" + error.stack);
data = { username, email, address, pass1 };
res.render("user.ejs", { data })
})
} catch (error) {
console.error('error in data base');
}
}
});
app.post("/user", (req, res) => {
let { email, password } = req.body;
q = `select * from users where email='${email}'`;
try {
connection.query(q, (error, result) => {
if (error) res.send("user not found");
let data = result[0];
if (password == data.password) {
console.log(data)
res.render("user.ejs", { data })
} else {
res.render("wrongpassword.ejs");
}
})
} catch (error) {
console.log("error found");
}
})
app.get("/user/donation/:email", (req, res) => {
let { email } = req.params;
q = `select * from users where email='${email}'`;
try {
connection.query(q, (error, result) => {
if (error) res.send("user not found");
let data = result[0];
res.render("donationform.ejs", { data });
})
} catch (error) {
console.log("error found");
}
})
app.post("/user/donation", (req, res) => {
let { name, email, mobile, address, date, time, comment } = req.body;
let q = `INSERT INTO donation (name, email, mobile, address, date, time, comment) VALUES ('${name}','${email}','${mobile}','${address}','${date}','${time}','${comment}')`;
try {
connection.query(q, (error, result) => {
if (error) {
res.send("Some error in the database: " + error.stack);
} else {
res.render("thanks.ejs");
}
});
} catch (error) {
console.error('Error in the database:', error);
}
});
app.get("/user/notification/:username",(req,res)=>{
let{username}=req.params;
q = `select * from donation`;
try {
connection.query(q, (error, result) => {
if (error) res.send("user not found");
let data = {username,result};
console.log(data)
res.render("donation.ejs", { data });
})
} catch (error) {
console.log("error found");
}
})