-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (96 loc) · 3.02 KB
/
app.js
File metadata and controls
133 lines (96 loc) · 3.02 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
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
//Import The module
import express from 'express'
import mysql2 from 'mysql2'
import dotenv from 'dotenv'
// Load the variables from .env file
dotenv.config()
const pool = mysql2.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT
}).promise()
//Create an instance of an Express application
const app = express()
//Set EJS as view engine
// app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// Enable static file serving
app.use(express.static('public'))
//Allow the app tp parse from data (req.body)
app.use(express.urlencoded({ extended: true }))
//Define the port number where our server will listen
const PORT = 3002
const orders = []
//Define a default "route"('/')
// req: contains information about the incoming request
// res: allows us to send back a response to the client
app.get('/', (req, res) => {
// Send a response to the client
//res.sendFile(`${import.meta.dirname}/views/home.html`)
res.render('home')
})
// CONTACT PAGE route
app.get('/contact', (req, res) => {
res.render('contact')
})
//Define an admin route
app.get('/admin', async (req, res) => {
try {
const [orders] = await
pool.query('SELECT * FROM contacts ORDER BY created_at DESC')
res.render('admin', { orders })
} catch (err) {
console.error('Database error:', err)
}
})
// Define the submit route
app.post('/submit-order', async (req, res) => {
//create a JSON object to store the data
const order = req.body
order.timestamp = new Date()
// Canonical checkbox logic
const subscribeChecked = req.body.subscribe === 'yes'
order.subscribe = subscribeChecked ? 'yes' : 'no'
// Canonical format logic
// If they didn’t subscribe, ignore any format and store null
const chosenFormat = subscribeChecked ? (req.body.format || null) : null
order.format = chosenFormat
//Write a query to insert order into DB
const sql = "INSERT INTO contacts (fname,lname,jobt,company,lurl,email,meet,otherinput,message,subscribe,format) VALUES(?,?,?,?,?,?,?,?,?,?,?)"
console.log(orders)
//Create array of Parameters of each placeholder
const params = [
order.fname || null,
order.lname || null,
order.jobt || null,
order.company || null,
order.lurl || null,
order.email || null,
order.meet || null,
order.otherinput || null,
order.message || null,
order.subscribe,
order.format
]
try {
console.log('REQ BODY:', req.body)
console.log('ORDER.SUBSCRIBE:', order.subscribe)
const [result] = await pool.execute(sql, params)
//Send User to confirmation page
res.render('confirmation', { order })
} catch (err) {
console.log("Database Error", err)
res.status(500).send('Database error')
}
})
app.get('/confirmation', (req, res) => {
const order = orders[orders.length - 1] || null
res.render('confirmation', { order })
})
// Start the server and make it listen on the port
// specified above
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`)
})