5. Babel Coder
Babel Coder
https://www.babelcoder.com
RESTFUL API
C 1 C 2 C 2 C 2
RESTful API คือเว็บเซอร์วิสรูปแบบหนึ่งที่กำหนดให้ API ประกอบด้วยทรัพยากรที่ Client สามารถร้องขอเพื่อเข้าถึงได้ โดย
Client จะทำการระบุทรัพยากรที่ต้องการเข้าถึงผ่าน URL และระบุการดำเนินการผ่าน HTTP Method
/products
Product
Order
User
Comment
URL
Method GET
เมื่อ API ประมวลผลเสร็จสิ้นจะคืนผลลัพธ์กลับมาในฟอร์แมตต่าง ๆ เช่น JSON เพื่อให้ Client ทราบว่าผลลัพธ์การทำงานเป็นเช่น
ไร API ต้องส่ง HTTP Status Code กลับด้วย พร้อมระบุ MIME Type เพื่อบอกชนิดของข้อมูลที่ส่งกลับ
MIME Type
Status
Response
application/json
200 OK
6. Babel Coder
Babel Coder
https://www.babelcoder.com
HTTP
Method
URL ความหมาย Status เมื่อสำเร็จ Status เมื่อไม่สำเร็จ
GET /products ร้องขอข้อมูล Products ทั้งหมด 200 OK
GET /products/1 ร้องขอข้อมูล Product ที่มี ID เป็น 1 200 OK 404 NOT FOUND
POST /products สร้างข้อมูล Product ตัวใหม่ 201 CREATED
400 BAD REQUEST
422 UNPROCESSIBLE ENTITY
PATCH /products/1 อัพเดทข้อมูลบางส่วนของ Product ที่มี ID เป็น 1 200 OK
400 BAD REQUEST
422 UNPROCESSIBLE ENTITY
DELETE /products/1 ลบข้อมูลของ Product ที่มี ID เป็น 1
200 OK
204 NO CONTENT
404 NOT FOUND
RESTFUL API
11. Babel Coder
Babel Coder
https://www.babelcoder.com
imports
Products
Products Controller Products Service
injec
t
UsersModul
e
providers ProductsService
imports
Auth
Auth Controller Auth Service
inject
UsersModule
providers AuthService
exports AuthService
@Global()
MODULES
Users Controller Users Service
providers
exports
Users
inject
UsersService
UsersService
inject
Global Modules สามารถใช้งานได้เลยโดยไม่ต้อง
ทำการระบุในส่วนของ imports
controllers UsersController
controllers AuthController
controllers ProductsController
12. Babel Coder
Babel Coder
https://www.babelcoder.com
SETTING UP
$ npx @nestjs/cli new
api
Which package manager would you ❤️ to use? pnpm
ทำการสร้างโปรเจคชื่อ api ผ่าน Nest CLI โดย
เลือกใช้ Package Manager เป็น pnpm
api
nest-cli.json
src
app.controller.ts
app.module.ts
app.service.ts
main.ts
App Module
ไฟล์การทำงานหลัก สามารถตั้งค่าการทำงานแบบ Global ได้ในไฟล์นี้
ไฟล์ตั้งค่าการทำงานของ Nest CLI