Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 167e9e3c authored by 65160394's avatar 65160394
Browse files

test

parent 13ad3a4c
Branches
No related tags found
No related merge requests found
DB_HOST=10.104.21.87
DB_PORT=3306
DB_USER=root
DB_PASSWORD=DCRmgr02120
DB_NAME=tourdb
\ No newline at end of file
require("dotenv").config(); const express = require('express');
const express = require("express");
const mysql = require('mysql2/promise'); const mysql = require('mysql2/promise');
const bodyParser = require("body-parser"); const path = require('path');
// สร้างแอป Express
const app = express(); const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json()); // ตั้งค่า view engine เป็น EJS
app.use(express.urlencoded({ extended: true })); app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Database configuration // สร้างการเชื่อมต่อฐานข้อมูล MySQL
const pool = mysql.createPool({ const pool = mysql.createPool({
host: process.env.DB_HOST , host: process.env.DB_HOST ,
port: process.env.DB_PORT , port: process.env.DB_PORT ,
...@@ -35,30 +35,29 @@ async function testConnection() { ...@@ -35,30 +35,29 @@ async function testConnection() {
} }
testConnection(); testConnection();
// ตั้งค่า Middleware // หน้าหลัก
app.set("view engine", "ejs"); app.get('/', async (req, res) => {
app.use(bodyParser.urlencoded({ extended: true })); try {
app.use(express.static("public")); const [rows] = await pool.query('SELECT * FROM tours');
res.render('index', { tours: rows });
// ดึงรายการทัวร์ } catch (err) {
app.get("/", (req, res) => { res.status(500).send('Server error');
// pool.query("SELECT * FROM tours", (err, results) => { }
// if (err) throw err;
res.render("index");
}); });
// });
// เพิ่มทัวร์ใหม่ // หน้ารายละเอียดทัวร์
app.post("/add", (req, res) => { app.get('/tour/:id', async (req, res) => {
const { name, location, price, description } = req.body; const { id } = req.params;
const sql = "INSERT INTO tours (name, location, price, description) VALUES (?, ?, ?, ?)"; try {
pool.query(sql, [name, location, price, description], (err) => { const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]);
if (err) throw err; res.render('tour', { tour: rows[0] });
res.redirect("/"); } catch (err) {
}); res.status(500).send('Server error');
}
}); });
// เริ่มเซิร์ฟเวอร์ // ตั้งค่าพอร์ต
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`); console.log(`Server is running on port ${PORT}`);
}); });
\ No newline at end of file
...@@ -3,18 +3,14 @@ ...@@ -3,18 +3,14 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tour & Travel</title> <title>Tour Website</title>
</head> </head>
<body> <body>
<h1>รายการทัวร์</h1> <h1>Welcome to our Tour Website</h1>
<ul>
<h2>เพิ่มทัวร์ใหม่</h2> <% tours.forEach(tour => { %>
<form action="/add" method="POST"> <li><a href="/tour/<%= tour.id %>"><%= tour.name %></a> - <%= tour.price %> THB</li>
<input type="text" name="name" placeholder="ชื่อทัวร์" required> <% }) %>
<input type="text" name="location" placeholder="สถานที่" required> </ul>
<input type="number" name="price" placeholder="ราคา" required>
<textarea name="description" placeholder="รายละเอียด"></textarea>
<button type="submit">เพิ่มทัวร์</button>
</form>
</body> </body>
</html> </html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= tour.name %></title>
</head>
<body>
<h1><%= tour.name %></h1>
<p><%= tour.description %></p>
<p>Price: <%= tour.price %> THB</p>
<img src="<%= tour.image_url %>" alt="<%= tour.name %>">
<br>
<a href="/">Back to Home</a>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment