-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (56 loc) · 1.9 KB
/
Copy pathserver.js
File metadata and controls
69 lines (56 loc) · 1.9 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mysql = require('mysql');
const app = express();
const port = 4000;
app.use(cors());
app.use(bodyParser.json());
app.use(express.static('docs'));
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'testimonial'
});
db.connect(err => {
if (err) {
console.error('Database connection failed:', err.stack);
return;
}
console.log('Connected to MySQL Database');
});
// to handle contact form submissions
app.post('/contact', (req, res) => {
const { name, email, message } = req.body;
console.log(`Contact Form Submission: Name: ${name}, Email: ${email}, Message: ${message}`);
res.json({ message: 'Your message has been sent successfully!' });
});
// to add a new testimonial
app.post('/addTestimonial', (req, res) => {
const { name, testimonial } = req.body;
const sql = 'INSERT INTO testimonials (name, testimonial) VALUES (?, ?)';
db.query(sql, [name, testimonial], (err, result) => {
if (err) {
console.error('Error inserting testimonial:', err);
res.status(500).send({ error: 'Failed to add testimonial' });
return;
}
res.json({ message: 'Testimonial added successfully' });
});
});
// to fetch all testimonials
app.get('/testimonials', (req, res) => {
const sql = 'SELECT * FROM testimonials';
db.query(sql, (err, results) => {
if (err) {
console.error('Error fetching testimonials:', err);
res.status(500).send({ error: 'Failed to fetch testimonials' });
return;
}
res.json(results);
});
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});