-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (79 loc) · 2.82 KB
/
app.js
File metadata and controls
103 lines (79 loc) · 2.82 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import express from 'express';
import mariadb from 'mariadb';
import validateForm from './services/validation.js';
import dotenv from 'dotenv';
const pool = mariadb.createPool({
host: 'localhost',
user: 'root',
password: '795594',
database: 'guestBook'
});
async function connect() {
try {
const conn = await pool.getConnection();
console.log('Connected to the database');
return conn;
} catch (err) {
console.log('Error connecting to the database: ' + err)
}
}
const app = express();
app.use(express.urlencoded({ extended: true }));
const signatures = [];
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.set('views', './views');
const PORT = 3000;
//render using ejs
app.get('/', (req, res) => {
res.render(`home`);
});
app.post('/signed', async (req, res) => {
if (!req.body.fname || !req.body.lname || !req.body.eAddress) {
return res.send('Invalid Input: First Name, Last Name, and Email are required.');
}
const sign = {
fname: req.body.fname,
lname: req.body.lname,
jobTitle: req.body.jobTitle,
co: req.body.co,
linkedInURL: req.body.linkedInURL,
eAddress: req.body.eAddress,
weMetAt: req.body.weMetAt !== 'Please Select...' ? req.body.weMetAt : req.body.other || "Not specified",
other: req.body.other,
message: req.body.message,
mailingList: req.body.mailingList ? "Yes" : "No",
emailFormat: req.body.emailFormat || "Not specified",
timestamp: new Date().toLocaleString() // Add timestamp to each submission
};
signatures.push(sign);
console.log(signatures);
const conn = await connect();
try {
const result = await conn.query(`
INSERT INTO signatures (
fname, lname, jobTitle, co, linkedInUrl, eAddress,
weMetAt, other, message, mailingList, emailFormat
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
sign.fname, sign.lname, sign.jobTitle, sign.co,
sign.linkedInURL, sign.eAddress, sign.weMetAt, sign.other,
sign.message, sign.mailingList, sign.emailFormat
]
);
res.render('signed', { sign });
} catch (err) {
console.log('Database error:', err);
res.send("Error saving your signature. Please try again.");
}
});
//render using ejs
app.get('/admin', async (req, res) => {
const conn = await connect();
const signatures = await conn.query('SELECT * FROM signatures;');
console.log(signatures);
res.render('admin', { signatures });
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});