-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
415 lines (368 loc) · 14.2 KB
/
Copy pathapp.js
File metadata and controls
415 lines (368 loc) · 14.2 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// app.js - Lab 6: Middleware และ Advanced Routing
const express = require('express');
const path = require('path');
// Import custom middleware
const { basicLogger, detailedLogger, errorLogger, performanceLogger, countRequests, rateLimit, requestStats } = require('./middleware/logger');
const { loginMiddleware, authenticateMiddleware, authorizeMiddleware, logoutMiddleware } = require('./middleware/auth');
const { userValidation, loginValidation, createValidationSchema } = require('./middleware/validation');
console.log('=== Lab 6: Middleware และ Advanced Routing ===\n');
const app = express();
const PORT = process.env.PORT || 3000;
// ============ GLOBAL MIDDLEWARE ============
// Built-in middleware
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
// Custom middleware
app.use(countRequests); // Count requests
app.use(basicLogger); // Basic logging
app.use(performanceLogger); // Performance monitoring
// Rate limiting (global)
app.use('/api/', rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: 'Too many API requests, please try again later'
}));
// Custom middleware examples
app.use((req, res, next) => {
req.requestTime = new Date();
req.customData = {
userAgent: req.get('User-Agent'),
acceptLanguage: req.get('Accept-Language'),
host: req.get('Host')
};
next();
});
// ============ ROUTES ============
// หน้าหลัก
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lab 6: Middleware Demo</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
line-height: 1.6;
background: linear-gradient(135deg, #8B5CF6 0%, #06B6D4 100%);
color: white;
min-height: 100vh;
}
.container {
max-width: 900px;
margin: 0 auto;
background: rgba(255,255,255,0.95);
color: #333;
padding: 30px;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
h1 { color: #8B5CF6; text-align: center; }
.middleware-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin: 20px 0;
}
.demo-card {
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
border-left: 4px solid #8B5CF6;
}
.demo-card h3 { margin-top: 0; color: #8B5CF6; }
.demo-card a {
color: #06B6D4;
text-decoration: none;
font-weight: bold;
}
.status {
background: #d4edda;
border: 1px solid #c3e6cb;
padding: 15px;
border-radius: 8px;
margin: 20px 0;
}
.curl-example {
background: #1a1a1a;
color: #00ff00;
padding: 15px;
border-radius: 5px;
font-family: monospace;
overflow-x: auto;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🔧 Middleware & Routing Lab</h1>
<div class="status">
<strong>📊 Request Stats:</strong><br>
Request #${req.requestCount} | Total: ${req.stats.total}<br>
<strong>🕒 Request Time:</strong> ${req.requestTime.toLocaleString('th-TH')}<br>
<strong>🌐 User Agent:</strong> ${req.customData.userAgent}
</div>
<h2>🔧 Middleware Demonstrations</h2>
<div class="middleware-demo">
<div class="demo-card">
<h3>🔐 Authentication</h3>
<p><a href="/api/auth/login">POST /api/auth/login</a></p>
<p>ทดสอบการ login และรับ token</p>
<div class="curl-example">
curl -X POST http://localhost:3000/api/auth/login \\
-H "Content-Type: application/json" \\
-d '{"username":"admin","password":"admin123"}'
</div>
</div>
<div class="demo-card">
<h3>👤 Protected Routes</h3>
<p><a href="/api/protected/profile">GET /api/protected/profile</a></p>
<p>เข้าถึงได้เฉพาะผู้ที่ login แล้ว</p>
<div class="curl-example">
curl -X GET http://localhost:3000/api/protected/profile \\
-H "Authorization: Bearer YOUR_TOKEN"
</div>
</div>
<div class="demo-card">
<h3>🛡️ Admin Only</h3>
<p><a href="/api/admin/users">GET /api/admin/users</a></p>
<p>เข้าถึงได้เฉพาะ admin</p>
<div class="curl-example">
curl -X GET http://localhost:3000/api/admin/users \\
-H "Authorization: Bearer ADMIN_TOKEN"
</div>
</div>
<div class="demo-card">
<h3>✅ Validation</h3>
<p><a href="/api/users/create">POST /api/users/create</a></p>
<p>ทดสอบ input validation</p>
<div class="curl-example">
curl -X POST http://localhost:3000/api/users/create \\
-H "Content-Type: application/json" \\
-d '{"name":"สมชาย","email":"somchai@example.com","age":25}'
</div>
</div>
<div class="demo-card">
<h3>⚡ Rate Limiting</h3>
<p><a href="/api/test/rate-limit">GET /api/test/rate-limit</a></p>
<p>ทดสอบ rate limiting (100 req/15min)</p>
</div>
<div class="demo-card">
<h3>📊 Request Stats</h3>
<p><a href="/api/stats">GET /api/stats</a></p>
<p>ดูสถิติการเรียกใช้ API</p>
</div>
<div class="demo-card">
<h3>💥 Error Demo</h3>
<p><a href="/api/test/error">GET /api/test/error</a></p>
<p>ทดสอบ error handling middleware</p>
</div>
<div class="demo-card">
<h3>🐌 Slow Route</h3>
<p><a href="/api/test/slow">GET /api/test/slow</a></p>
<p>ทดสอบ performance monitoring</p>
</div>
</div>
<h2>📚 Available Test Accounts</h2>
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px;">
<strong>Admin:</strong> username: admin, password: admin123<br>
<strong>User:</strong> username: user, password: user123<br>
<strong>Guest:</strong> username: guest, password: guest123
</div>
</div>
</body>
</html>
`);
});
// ============ AUTHENTICATION ROUTES ============
// Login route with validation
app.post('/api/auth/login', loginValidation, loginMiddleware, (req, res) => {
res.json({
success: true,
message: 'เข้าสู่ระบบสำเร็จ',
data: {
token: req.token,
user: {
id: req.user.id,
username: req.user.username,
role: req.user.role
}
}
});
});
// Logout route
app.post('/api/auth/logout', logoutMiddleware, (req, res) => {
res.json({
success: true,
message: req.loggedOut ? 'ออกจากระบบสำเร็จ' : 'ไม่พบ session'
});
});
// ============ PROTECTED ROUTES ============
// Protected routes (ต้อง login)
app.get('/api/protected/profile', authenticateMiddleware, (req, res) => {
res.json({
success: true,
message: 'ข้อมูลโปรไฟล์',
data: {
user: req.user,
session: req.session,
requestTime: req.requestTime
}
});
});
app.get('/api/protected/dashboard', authenticateMiddleware, (req, res) => {
res.json({
success: true,
message: 'Dashboard data',
data: {
welcomeMessage: `สวัสดี ${req.user.username}!`,
lastLogin: req.session.loginTime,
userRole: req.user.role
}
});
});
// ============ ADMIN ONLY ROUTES ============
// Admin only routes
app.get('/api/admin/users', authenticateMiddleware, authorizeMiddleware(['admin']), (req, res) => {
res.json({
success: true,
message: 'ข้อมูลผู้ใช้ทั้งหมด (Admin only)',
data: [
{ id: 1, username: 'admin', role: 'admin' },
{ id: 2, username: 'user', role: 'user' },
{ id: 3, username: 'guest', role: 'guest' }
]
});
});
app.get('/api/admin/logs', authenticateMiddleware, authorizeMiddleware(['admin']), detailedLogger, (req, res) => {
res.json({
success: true,
message: 'System logs (Admin only)',
data: {
requestStats,
uptime: process.uptime(),
memory: process.memoryUsage()
}
});
});
// ============ VALIDATION DEMO ROUTES ============
// User creation with validation
app.post('/api/users/create', userValidation, (req, res) => {
res.json({
success: true,
message: 'ผู้ใช้ใหม่ถูกสร้างสำเร็จ',
data: req.validatedData,
note: 'ข้อมูลผ่านการ validate แล้ว'
});
});
// Custom validation example
const customValidation = createValidationSchema({
title: {
required: true,
type: 'string',
minLength: 5,
maxLength: 100
},
content: {
required: true,
type: 'string',
minLength: 10
},
category: {
required: true,
type: 'string',
custom: (value) => {
const allowedCategories = ['tech', 'lifestyle', 'education', 'news'];
if (!allowedCategories.includes(value)) {
return `หมวดหมู่ต้องเป็น: ${allowedCategories.join(', ')}`;
}
return true;
}
}
});
app.post('/api/posts/create', customValidation, (req, res) => {
res.json({
success: true,
message: 'โพสต์ถูกสร้างสำเร็จ',
data: req.validatedData
});
});
// ============ TEST ROUTES ============
// Rate limit test
app.get('/api/test/rate-limit', (req, res) => {
res.json({
success: true,
message: 'Rate limit test',
headers: {
'X-RateLimit-Limit': res.get('X-RateLimit-Limit'),
'X-RateLimit-Remaining': res.get('X-RateLimit-Remaining'),
'X-RateLimit-Reset': res.get('X-RateLimit-Reset')
}
});
});
// Error test
app.get('/api/test/error', (req, res, next) => {
const error = new Error('ข้อผิดพลาดทดสอบ');
error.status = 500;
next(error);
});
// Slow route test
app.get('/api/test/slow', (req, res) => {
setTimeout(() => {
res.json({
success: true,
message: 'Slow response (2 seconds delay)',
timestamp: new Date().toISOString()
});
}, 2000);
});
// Stats route
app.get('/api/stats', (req, res) => {
res.json({
success: true,
data: {
...requestStats,
uptime: process.uptime(),
memory: process.memoryUsage(),
currentRequests: req.requestCount
}
});
});
// ============ ERROR HANDLING MIDDLEWARE ============
// 404 handler
app.use((req, res, next) => {
res.status(404).json({
success: false,
message: 'ไม่พบ endpoint ที่ต้องการ',
path: req.url,
method: req.method
});
});
// Error handling middleware
app.use(errorLogger);
app.use((error, req, res, next) => {
res.status(error.status || 500).json({
success: false,
message: error.message || 'เกิดข้อผิดพลาดภายในเซิร์ฟเวอร์',
requestId: req.requestCount,
timestamp: req.requestTime
});
});
// ============ SERVER STARTUP ============
app.listen(PORT, () => {
console.log(`🚀 Middleware Lab server กำลังทำงานที่ http://localhost:${PORT}`);
console.log('📋 เปิดเบราว์เซอร์ไปที่ URL ข้างต้นเพื่อดู demo');
console.log('📊 ทดสอบ middleware ต่าง ๆ ได้');
console.log('\n💡 กด Ctrl+C เพื่อหยุดเซิร์ฟเวอร์\n');
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n🛑 กำลังปิดเซิร์ฟเวอร์...');
console.log(`📊 สถิติ: ${requestStats.total} requests ทั้งหมด`);
console.log('✅ เซิร์ฟเวอร์ปิดเรียบร้อยแล้ว');
process.exit(0);
});