Gitlab@Informatics

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

test

parent 13ad3a4c
No related branches found
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 bodyParser = require("body-parser");
const path = require('path');
// สร้างแอป Express
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// ตั้งค่า view engine เป็น EJS
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Database configuration
// สร้างการเชื่อมต่อฐานข้อมูล MySQL
const pool = mysql.createPool({
host: process.env.DB_HOST ,
port: process.env.DB_PORT ,
......@@ -35,30 +35,29 @@ async function testConnection() {
}
testConnection();
// ตั้งค่า Middleware
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// ดึงรายการทัวร์
app.get("/", (req, res) => {
// pool.query("SELECT * FROM tours", (err, results) => {
// if (err) throw err;
res.render("index");
// หน้าหลัก
app.get('/', async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM tours');
res.render('index', { tours: rows });
} catch (err) {
res.status(500).send('Server error');
}
});
// });
// เพิ่มทัวร์ใหม่
app.post("/add", (req, res) => {
const { name, location, price, description } = req.body;
const sql = "INSERT INTO tours (name, location, price, description) VALUES (?, ?, ?, ?)";
pool.query(sql, [name, location, price, description], (err) => {
if (err) throw err;
res.redirect("/");
});
// หน้ารายละเอียดทัวร์
app.get('/tour/:id', async (req, res) => {
const { id } = req.params;
try {
const [rows] = await pool.query('SELECT * FROM tours WHERE id = ?', [id]);
res.render('tour', { tour: rows[0] });
} catch (err) {
res.status(500).send('Server error');
}
});
// เริ่มเซิร์ฟเวอร์
// ตั้งค่าพอร์ต
const PORT = process.env.PORT || 3000;
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 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tour & Travel</title>
<title>Tour Website</title>
</head>
<body>
<h1>รายการทัวร์</h1>
<h2>เพิ่มทัวร์ใหม่</h2>
<form action="/add" method="POST">
<input type="text" name="name" placeholder="ชื่อทัวร์" required>
<input type="text" name="location" placeholder="สถานที่" required>
<input type="number" name="price" placeholder="ราคา" required>
<textarea name="description" placeholder="รายละเอียด"></textarea>
<button type="submit">เพิ่มทัวร์</button>
</form>
<h1>Welcome to our Tour Website</h1>
<ul>
<% tours.forEach(tour => { %>
<li><a href="/tour/<%= tour.id %>"><%= tour.name %></a> - <%= tour.price %> THB</li>
<% }) %>
</ul>
</body>
</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