Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160394
Project
Commits
167e9e3c
Commit
167e9e3c
authored
2 months ago
by
65160394
Browse files
Options
Downloads
Patches
Plain Diff
test
parent
13ad3a4c
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.env
+0
-5
0 additions, 5 deletions
.env
server.js
+30
-31
30 additions, 31 deletions
server.js
views/index.ejs
+9
-13
9 additions, 13 deletions
views/index.ejs
views/tour.ejs
+16
-0
16 additions, 0 deletions
views/tour.ejs
with
55 additions
and
49 deletions
.env
deleted
100644 → 0
+
0
−
5
View file @
13ad3a4c
DB_HOST=10.104.21.87
DB_PORT=3306
DB_USER=root
DB_PASSWORD=DCRmgr02120
DB_NAME=tourdb
\ No newline at end of file
This diff is collapsed.
Click to expand it.
server.js
+
30
−
31
View file @
167e9e3c
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
This diff is collapsed.
Click to expand it.
views/index.ejs
+
9
−
13
View file @
167e9e3c
...
...
@@ -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>
This diff is collapsed.
Click to expand it.
views/tour.ejs
0 → 100644
+
16
−
0
View file @
167e9e3c
<!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>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment