TypeScript OOP Quiz 4 Master Guide (Section 34 + 35) สำหรับอ่านสอบแบบจบในไฟล์เดียว
Updated: 2026-03-24
0) วิธีใช้ไฟล์นี้ให้คุ้มที่สุดก่อนสอบ
- อ่านหัวข้อ 1-2 เพื่อจับภาพรวมโปรเจกต์และ endpoint ทั้งหมด
- อ่านหัวข้อ 3-4 เพื่อเข้าใจการทำงานของทุก method แบบบรรทัดต่อบรรทัด (Controller + Service)
- อ่านหัวข้อ 5 เพื่อจำทางลัดโค้ดที่ออกสอบบ่อย (หา id ล่าสุด, อ่าน/เขียนไฟล์, filter fields, redaction)
- อ่านหัวข้อ 6-8 เพื่อเตรียมพร้อมลงมือจริง (curl test, checklist, extension VS Code)
1) ภาพรวมทั้ง 2 โปรเจกต์ใน workspace
คุณมี 2 โปรเจกต์ NestJS ที่ใช้รูปแบบเดียวกัน (Controller + Service + Module + JSON file storage)
typescript-oop-quiz04-sec34-IkrtI
- ธีม: Missions API
- Data file:
data/missions.json
- Core file:
src/mission/mission.controller.ts, src/mission/mission.service.ts
typescript-oop-quiz04-sec35-IkrtI
- ธีม: Users API
- Data file:
data/users.json
- Core file:
src/user/user.controller.ts, src/user/user.service.ts, src/user/dto/create-user.dto.ts
จุดร่วมที่ออกสอบบ่อยมาก:
- อ่านไฟล์ JSON -> แปลงข้อมูล -> ส่ง response
- หา record จาก id และ throw 404 ถ้าไม่เจอ
- สร้างข้อมูลใหม่โดย generate id ถัดไป
- เขียน JSON กลับลงไฟล์ด้วย
JSON.stringify(data, null, 2)
- Route static ต้องมาก่อน route dynamic