-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (43 loc) · 1.35 KB
/
server.js
File metadata and controls
52 lines (43 loc) · 1.35 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
// server.js
const express = require("express");
const mysql = require("mysql2");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
const PORT = 3000;
// Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve static files (HTML, CSS, etc.) from current directory
app.use(express.static(__dirname));
// MariaDB Connection
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "1998",
database: "latehar_tourism",
});
db.connect((err) => {
if (err) throw err;
console.log("Connected to MariaDB database.");
});
// Form POST handler
app.post("/submit", (req, res) => {
const { name, email, message } = req.body;
const sql = "INSERT INTO contacts (name, email, message) VALUES (?, ?, ?)";
db.query(sql, [name, email, message], (err, result) => {
if (err) {
console.error("Failed to insert data:", err);
return res.status(500).send("Error saving your message.");
}
res.send("Message saved successfully!");
});
});
// Optional route to serve contact.html as /contact
app.get("/contact", (req, res) => {
res.sendFile(path.join(__dirname, "contact.html"));
});
// Start Server
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});