forked from Harshadjoshi01/Developers-Hunt-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseIntigration.txt
88 lines (79 loc) · 3.11 KB
/
DatabaseIntigration.txt
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
var options = {
host: process.env.DATABASEHOST,
port: process.env.DATABASEPORT,
user: process.env.DATABASEUSER,
password: process.env.DATABASEPASSWORD,
database: process.env.DATABASENAME,
};
var db;
connectToDB = function () {
db = mysql.createConnection(options);
db.connect(function(err) {
if (err) {
setTimeout(connectToDB, 2000);
}
});
db.on('error', function(err) {
//Error message
systemMessage("Error: " + err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
connectToDB(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
};
systemMessage = function(message) {
console.log("====================================================");
console.log("Database is Reconnecting But the app will not crash");
console.log("====================================================");
};
connectToDB();
console.log("Application is Running");
app.post("/formdata", (req, res) => {
let stud_name = req.body.stud_name.toString();
let stud_dob = req.body.stud_dob.toString();
let stud_wp_num = req.body.stud_wp_num.toString();
let stud_email = req.body.stud_email.toString();
let stud_department = req.body.stud_department.toString();
let stud_year = req.body.stud_year.toString();
db.query("select stud_email from form_data where stud_email = ?", [stud_email], (err, data) => {
if(err){
console.log(err);
}
if(data.length == 0){
db.query("insert into form_data (stud_name, stud_dob, stud_wp_num, stud_email, stud_department, stud_year) values (?, ?, ?, ?, ?, ?)",
[stud_name,stud_dob,stud_wp_num,stud_email,stud_department,stud_year],
(err, data) => {
if (err){
console.log(err);
}
var msg = "Thank You "+stud_name+" for registering for Developer's Hunt 2021 Here is Our Whatsapp group Join It for further Updates "+ wp_link;
var mailOptions = {
from: process.env.EMAIL,
to: stud_email,
subject: 'Email From GRCS DEVELOPERS CLUB',
text: msg
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
req.session.message = {
type: "success",
message: 'Thank You '+stud_name+' successfully regestered Please Check your mailbox for Whatsaap group link'
}
res.redirect("/")
});
}else {
req.session.message = {
type: "danger",
message: 'Email already exists ! Try with different Email Id'
}
res.redirect("/")
}
});
});