ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
API ဆိုတာ program တစ်ခုက တခြား program တစ်ခုကို တစ်ခုခု တောင်းဖို့ ဘက်နှစ်ဖက်လုံး သဘောတူထားတဲ့ စည်းမျဉ်းတွေနဲ့ ဆက်သွယ်တဲ့ နည်းလမ်းပါ။ စားသောက်ဆိုင်ကို စဉ်းစားကြည့်ပါ။ မီးဖိုချောင်ထဲ ကိုယ်တိုင် ဝင်မသွားပါဘူး၊ menu ကို ဖတ်ပြီး waiter ကို ဘာလိုချင်လဲ ပြောလိုက်ရင် waiter က ဟင်းပွဲကို ယူလာပေးပါတယ်။ Menu က documentation၊ မှာလိုက်တဲ့ order က request၊ ရလာတဲ့ ဟင်းပွဲက response ပါ။
Web API ကလည်း အင်တာနက်ပေါ်မှာ ဒီလိုပဲ အလုပ်လုပ်ပါတယ်။ ကိုယ့် app က URL တစ်ခုကို HTTP request ပို့ပါတယ်၊ server က နောက်ကွယ်မှာ အလုပ်လုပ်ပြီး response ကို (များသောအားဖြင့် JSON နဲ့) ပြန်ပို့ပါတယ်။ Server ရဲ့ database ဒါမှမဟုတ် code ကို ကိုယ်က မမြင်ရပါဘူး၊ သူ ကမ်းလှမ်းထားတဲ့ menu ကိုပဲ မြင်ရတာပါ။
Problem → Why → Solution
ပြဿနာ: request တစ်ခု အလုပ်လုပ်တာနဲ့ ဘာမှ မသက်သေပြနိုင်ဘူး
ကိုယ့် app က ပစ္စည်းတစ်ခုရဲ့ လက်ရှိ ဈေးနှုန်း၊ user တစ်ယောက်ရဲ့ profile ဒါမှမဟုတ် ငွေပေးချေမှု ရလဒ်ကို လိုအပ်တယ်ဆိုပါစို့။ အဲဒီ အချက်အလက်တွေက တခြားသူရဲ့ ကွန်ပျူတာပေါ်မှာ ရှိပြီး ကိုယ်က ဝင်ယူလို့ မရပါဘူး။ မေးဖို့ သဘောတူထားတဲ့ နည်းလမ်း မရှိရင် program တစ်စုံစီအတွက် ကိုယ်ပိုင် ချိတ်ဆက်မှု တစ်ခုစီ လိုလာပါမယ်။
Client ရဲ့ ပထမဆုံး ကြိုးစားမှုက ကောင်းတဲ့ နေ့မှာ ပြဿနာ မရှိသလို မြင်ရပါတယ်။ Request တစ်ခုပို့၊ အဖြေဖတ်၊ ဆက်သွားပါတယ်။ ပြဿနာက happy path က လျစ်လျူရှုထားတာတွေပါ။ Server က error status နဲ့ ပြန်ဖြေနိုင်တယ်၊ body က မျှော်ထားတာနဲ့ မတူနိုင်ဘူး၊ network ပြတ်နိုင်တယ်။ အောက်က code က မရှိတဲ့ user ကို ပထမဆုံး တောင်းတာနဲ့ crash ဖြစ်ပါတယ်။
// api.example.com is a placeholder host used for illustration only
const res = await fetch("https://api.example.com/v1/users/42");
const user = await res.json();
console.log(user.name.toUpperCase());
// If user 42 does not exist, the server answers 404 with {"error":"not_found"}:
// TypeError: Cannot read properties of undefined (reading 'toUpperCase')Request တစ်ခု အလုပ်လုပ်တာ ဘာကြောင့် မလုံလောက်တာလဲ
Status 200 က request တစ်ခု၊ user တစ်ယောက်၊ key တစ်ခုနဲ့ တစ်ကြိမ် အလုပ်လုပ်ခဲ့တာကိုပဲ သက်သေပြပါတယ်။ မရှိတဲ့ record၊ သက်တမ်းကုန်တဲ့ credential၊ နှေးတဲ့ network၊ error ပြန်တဲ့ server တွေအကြောင်း ဘာမှ မပြောပါဘူး။ ဒီ course က status line၊ header နဲ့ body ကို သီးခြားစီ ဖတ်တတ်အောင် သင်ပေးမှာမို့ client က အံ့အားသင့်စရာ ဖြစ်တိုင်း crash မဖြစ်ဘဲ ရလဒ်တစ်မျိုးချင်းစီကို ရည်ရွယ်ချက်ရှိရှိ ကိုင်တွယ်နိုင်ပါလိမ့်မယ်။
Core concept: API ဆိုတာ program နှစ်ခုကြား contract ပါ
API (Application Programming Interface) ဆိုတာ program နှစ်ခုကြား contract တစ်ခုပါ။ ဘယ်လို request မျိုးကို server က နားလည်လဲ၊ အဖြေတစ်ခုစီက ဘယ်ပုံစံ ဖြစ်မလဲဆိုတာကို contract က ပြောထားပါတယ်။ ဘက်နှစ်ဖက်လုံးက သူ့ဘက်က contract ကို လိုက်နာကြတဲ့အတွက် client က server ကို Python နဲ့ ရေးထားလား၊ Go ဒါမှမဟုတ် Java နဲ့ ရေးထားလား၊ ဘယ် database သုံးလဲဆိုတာ သိစရာ မလိုပါဘူး။ ဒီ lesson နဲ့ course တစ်ခုလုံးက web API တွေအကြောင်းပါ၊ အဲဒီ contract ရဲ့ ဘာသာစကားအဖြစ် HTTP ကို သုံးတာပါ။
HTTP ဆက်သွယ်မှုတိုင်းမှာ အပိုင်းနှစ်ပိုင်း အတူတူပါပဲ။ Client က request ကို ပို့ပါတယ်၊ အဲဒီမှာ method (ဘာလုပ်မလဲ)၊ URL (ဘယ်အရာကို)၊ header (လက်ခံနိုင်တဲ့ format လို အပို အချက်အလက်) နဲ့ တစ်ခါတလေ body (ပို့တဲ့ data) ပါပါတယ်။ Server က response ပြန်ပို့ပါတယ်၊ အဲဒီမှာ status code (ဘယ်လို ဖြစ်သွားလဲ)၊ header နဲ့ အများအားဖြင့် body ပါပါတယ်။ API သုံးသူအနေနဲ့ ကိုယ့်အလုပ်က ပထမအပိုင်းကို ဂရုတစိုက် တည်ဆောက်ဖို့နဲ့ ဒုတိယအပိုင်းရဲ့ အစိတ်အပိုင်းသုံးခုလုံးကို ဖတ်ဖို့ပါ၊ body တစ်ခုတည်း မဟုတ်ပါဘူး။
Server က စကားစတင်တာ တစ်ခါမှ မရှိပါဘူး၊ request တွေကိုပဲ ပြန်ဖြေပါတယ်။ ဒါကြောင့် web API ကို request-response လို့ ခေါ်တာဖြစ်ပြီး ဒီနေရာက ဥပမာတိုင်းမှာ ဘက်နှစ်ဖက် ပါတာလည်း ဒါကြောင့်ပါ — server (mock) တစ်ခုနဲ့ အဲဒါကို ခေါ်တဲ့ client တစ်ခုပါ။ Client က ကိုယ်ရေးရတာ အများဆုံး code ပါ။ Server ကတော့ ကိုယ်မထိန်းချုပ်နိုင်တဲ့ payment ကုမ္ပဏီလို provider ဖြစ်နိုင်သလို Projects chapter မှာ ကိုယ်တိုင် ဆောက်မယ့် team ရဲ့ service လည်း ဖြစ်နိုင်ပါတယ်။
ဒီ course ထဲက ဥပမာတွေထဲမှာ internet connection၊ account ဒါမှမဟုတ် secret key လိုတာ တစ်ခုမှ မရှိပါဘူး။ Run လို့ရတဲ့ sample တစ်ခုစီက ကိုယ့်ကွန်ပျူတာပေါ်မှာ ကျပန်း အလွတ် port တစ်ခုပေါ် mock server လေး တစ်ခု စတင်ပြီး fetch နဲ့ ခေါ်၊ ပြီးရင် ပြန်ပိတ်ပါတယ်။ ဒါကြောင့် run တိုင်း output အတူတူ ဖြစ်ပြီး ကြိုက်သလို ချိုးဖျက်စမ်းလို့ ရပါတယ်။ နောက် lesson တွေမှာ ပေါ်လာမယ့် provider URL အစစ်တွေကို ဥပမာအဖြစ်ပဲ မှတ်ထားပါတယ်၊ provider အသေးစိတ်တွေ ပြောင်းလဲတတ်လို့ official documentation မှာ အမြဲ ပြန်စစ်သင့်ပါတယ်။
- Client က request ပို့တယ်
- Server က method နဲ့ path ကို ဖတ်တယ်
- Server က status, header, body ပြန်ပို့တယ်
- Client က body မဖတ်ခင် status ကို စစ်တယ်
Response ရဲ့ အပိုင်းသုံးခု (status, header, body) ကို အစဉ်လိုက် ဖတ်ပါ၊ body ကို နောက်ဆုံးမှ ဖတ်ပါ။
အထွေထွေ ပုံစံ: request တစ်ခုကို လိုင်းအနည်းငယ်နဲ့
const res = await fetch(url, { method, headers, body });
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
const data = await res.json();fetch(url, options) က request ကို စတင်ပြီး server ရဲ့ header တွေ ရောက်လာတာနဲ့ Response object နဲ့ resolve ဖြစ်ပါတယ်။ res.ok က status code 200 ကနေ 299 အထိ ဖြစ်ရင် true ပါ။ အဲဒီ စစ်ပြီးမှ res.json() နဲ့ body ကို parse လုပ်တာ ဘေးကင်းပါတယ်။ options object က မထည့်လည်း ရပါတယ်၊ မထည့်ရင် ရိုးရိုး GET ဖြစ်ပါတယ်။
Example: mock server ကို ပထမဆုံး request
ဒီ script က အခန်းကဏ္ဍနှစ်ခုလုံးကို file တစ်ခုတည်းမှာ ဆောင်ရွက်ပါတယ်။ /api/course မှာ course outline ကို ပေးတဲ့ mock API ကို စတင်ပြီး client အဖြစ် GET request ပို့ကာ status line နဲ့ header တစ်ခုကို print လုပ်၊ JSON body ကို ဖတ်လို့ရတဲ့ table အဖြစ် ပြောင်းပါတယ်။ first-request.mjs အဖြစ် save လုပ်ပြီး node first-request.mjs နဲ့ run ပါ (Node 22 ဒါမှမဟုတ် ပိုသစ်တာ)။ ဘာမှ ကိုယ့်စက်ပြင်ကို မထွက်ပါဘူး။
import http from "node:http";
const course = {
title: "API Tutorial",
chapters: [
{ name: "Basic", lessons: 5 },
{ name: "Intermediate", lessons: 9 },
{ name: "Advanced", lessons: 14 },
{ name: "Projects", lessons: 3 },
{ name: "Exercises", lessons: 2 },
],
};
// The mock server: GET /api/course returns JSON, every other request gets a 404.
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/api/course") {
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
res.end(JSON.stringify(course));
} else {
res.writeHead(404, { "Content-Type": "application/json; charset=utf-8" });
res.end(JSON.stringify({ error: "not_found" }));
}
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
// The client: send one request, then read status, header and body separately.
const res = await fetch(`${base}/api/course`);
console.log("Status:", res.status, res.statusText);
console.log("Content-Type:", res.headers.get("content-type"));
const data = await res.json();
let total = 0;
for (const { name, lessons } of data.chapters) {
console.log(name.padEnd(13) + String(lessons).padStart(2) + " lessons");
total += lessons;
}
console.log("Total".padEnd(13) + String(total).padStart(2) + " lessons");
server.close();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Route check
if (req.method === "GET" && req.url === "/api/course") {Server က request ရဲ့ method နဲ့ URL path ကို ကြည့်ပြီး ဘာလုပ်မလဲ ဆုံးဖြတ်ပါတယ်။ ဒီ နှစ်ခုလုံး ကိုက်မှ course ကို ပြန်ပေးပြီး တခြား ဟာအားလုံးက 404 branch ကို ရောက်ပါတယ်။
- Status and type
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });Response ရဲ့ status code နဲ့ header ကို body မရေးခင် သတ်မှတ်ပါတယ်။ Content-Type header က client ကို body က JSON ဖြစ်ကြောင်း ပြောပြပါတယ်။
- Port 0
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));Port 0 ဆိုတာ operating system ကို အလွတ် port တစ်ခု ရွေးခိုင်းတာပါ၊ တခြား program နဲ့ မတိုက်ပါဘူး။ 127.0.0.1 ဆိုတာ ကိုယ့်စက်ကပဲ ခေါ်လို့ရတယ်လို့ ဆိုလိုပြီး listen ပြီးမှ ဆက်သွားဖို့ await လုပ်ထားပါတယ်။
- Send the request
const res = await fetch(`${base}/api/course`);fetch က server ရဲ့ header တွေ ရောက်တာနဲ့ Response object ကို ပြန်ပေးပါတယ်။ ဒီအချိန်မှာ body ကို မဖတ်ရသေးပါဘူး၊ status နဲ့ header တွေကိုပဲ ကြည့်လို့ရပါပြီ။
- Read the head first
console.log("Status:", res.status, res.statusText);body ကို မဖတ်ခင် status ကို အရင် ဖတ်တာက ကောင်းတဲ့ အကျင့်ပါ။ res.status က နံပါတ် (200) ဖြစ်ပြီး res.statusText က စာသား (OK) ဖြစ်ပါတယ်။
- Parse the body
const data = await res.json();Body က stream အဖြစ် ရောက်လာလို့ ဒုတိယ await လိုပါတယ်။ res.json() က စာသားကို JSON အဖြစ် parse လုပ်ပေးပြီး JSON မမှန်ရင် SyntaxError နဲ့ reject ဖြစ်ပါတယ်။
- Shut down
server.close();ပိတ်မထားရင် listen နေတဲ့ server က Node process ကို ဘယ်တော့မှ မပြီးအောင် ဆက်ထားပါလိမ့်မယ်။ Script က အဆုံးသတ်ရင် ပိတ်တာ မမေ့ပါနဲ့။
Status: 200 OK
Content-Type: application/json; charset=utf-8
Basic 5 lessons
Intermediate 9 lessons
Advanced 14 lessons
Projects 3 lessons
Exercises 2 lessons
Total 33 lessons- Request ကို တည်ဆောက်
- fetch နဲ့ ပို့
- Status နဲ့ header ကို ဖတ်
- res.ok ကို စစ်
- Body ကို parse လုပ်
Status 2xx
res.json() and use the data
Status 4xx / 5xx
Handle the error status instead of parsing blindly
Chapter ငါးခုကို တစ်ချက်ကြည့်
Lesson 33 ခုကို chapter တစ်ခုက ရှေ့ chapter က သင်ထားတာကိုပဲ လိုအပ်အောင် စီထားပါတယ်။ Basic က ဘာသာစကားနဲ့ ကိရိယာတွေ ပေးပါတယ်၊ Intermediate က request တစ်ခုကို အတည်ပြုပြီး ဖတ်ရတဲ့ အပိုင်းတွေ ပေးပါတယ်၊ Advanced က အစစ်အမှန် service တွေမှာ ကြုံရမယ့် အပိုင်းတွေ ပေးပါတယ်။ Projects က အားလုံးကို အတူ အသုံးချပြီး Exercises က ကိုယ်တိုင် စမ်းဖို့ပါ။
| Chapter | Lesson | ဘာတွေ သင်မလဲ |
|---|---|---|
| Basic | 5 | API ဆိုတာ၊ REST၊ curl နဲ့ Postman၊ endpoint တစ်ခုရဲ့ အစိတ်အပိုင်းတွေ |
| Intermediate | 9 | Auth (API key, Bearer, OAuth 2.0)၊ HTTP method၊ response ပုံစံ၊ pagination၊ error handling |
| Advanced | 14 | User CRUD လေးခု၊ AI provider ခြောက်ခု၊ rate limit၊ CORS၊ versioning၊ OpenAPI |
| Projects | 3 | Mini project တစ်ခုကို setup၊ CRUD feature နဲ့ error handling အဆင့်ဆင့် |
| Exercises | 2 | Request အခြေခံ လေ့ကျင့်ခန်း၊ CRUD, pagination နဲ့ error ပေါင်းလေ့ကျင့်ခန်း |
Lesson တစ်ခုကို ဘယ်လို လေ့လာမလဲ
1. Node ကို စစ်ပါ
Terminal မှာ node --version ရိုက်ပါ။ ဒီ course က Node 22 ဒါမှမဟုတ် ပိုသစ်တာကို အသုံးပြုပြီး fetch က built-in ဖြစ်လို့ install လုပ်စရာ package မလိုပါဘူး။
2. Sample ကို ကိုယ်တိုင် ရိုက်ပါ
Code ကို .mjs file တစ်ခုထဲ save လုပ်ပါ။ UI ထဲက copy button နဲ့ ကူးယူလည်း ရပါတယ်၊ ဒါပေမယ့် ကိုယ်တိုင် ရိုက်ရင် ပိုမှတ်မိပါတယ်။
3. Run ပြီး တစ်ခုခု ပြောင်းပါ
node file.mjs နဲ့ run ပြီး output ကို နှိုင်းယှဉ်ပါ။ ပြီးရင် path ကို မှားရေးကြည့်ပါ၊ status ဘယ်လို ပြောင်းသွားလဲ ကြည့်ပါ။
node --version
node first-request.mjsလက်တွေ့ project တွေမှာ
Real project တွေမှာ API တွေ ဘယ်နေရာ ရှိလဲ
ယနေ့ web app နဲ့ mobile app တိုင်းလိုလို API တွေပေါ်မှာ ဆောက်ထားပါတယ်။ Login screen က အကောင့်စစ်တဲ့ API ကို ခေါ်ပါတယ်၊ checkout page က ငွေပေးချေမှု provider ရဲ့ API ကို ခေါ်ပါတယ်၊ map က နေရာအချက်အလက် API ကို ခေါ်ပါတယ်၊ chat feature တွေက AI provider ရဲ့ API ကို ခေါ်ပါတယ်။ ကိုယ့်ကိုယ်ကိုယ် ရေးတဲ့ frontend နဲ့ backend ကြားကိုလည်း API နဲ့ပဲ ချိတ်ပါတယ်။
Demo ကနေ production ကို ရောက်တဲ့အခါ ပြဿနာအမျိုးအစား ပြောင်းသွားပါတယ်။ Credential တွေ သက်တမ်းကုန်တယ်၊ provider က request များလွန်းရင် ကန့်သတ်တယ်၊ version အသစ်က field နာမည်ပြောင်းတယ်၊ network တစ်ခါတလေ ပြတ်တယ်။ Team တွေက request/response တစ်ခုချင်းကို log ထဲ ရှာကြည့်နိုင်ဖို့ ကိရိယာတွေ (curl, Postman) သုံးပြီး ပြဿနာကို ပြန်ထုတ်ကြည့်ကြပါတယ်။ ဒီ course ရဲ့ ရည်ရွယ်ချက်က အဲဒီ အခြေအနေတွေကို ကြိုမြင်ပြီး လုံခြုံစွာ ချိတ်ဆက်တတ်လာဖို့ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Status ကို မစစ်ဘဲ body ကို သုံးတာ
404 ဒါမှမဟုတ် 500 ပြန်တဲ့အခါ body က error message ဖြစ်တတ်ပြီး မျှော်ထားတဲ့ field တွေ မပါပါဘူး။ ဒီအခါ user.name လို ဖတ်ရင် undefined ရပြီး နောက်ပိုင်း code မှာ TypeError ထွက်ပါတယ်၊ အမှားရဲ့ အကြောင်းရင်းနဲ့ crash ဖြစ်တဲ့ နေရာ အဝေးကြီး ကွာသွားပါတယ်။ res.ok ကို အရင်စစ်ပါ။
2. fetch က 404 မှာ error ပစ်မယ်လို့ ထင်တာ
fetch ရဲ့ promise က network ပြတ်တာလို request ကိုယ်တိုင် မအောင်မြင်မှသာ reject ဖြစ်ပါတယ်။ Server က 404 ဒါမှမဟုတ် 500 နဲ့ ဖြေရင် response ရောက်ပြီးမို့ resolve ဖြစ်ပါတယ်။ try/catch တစ်ခုတည်းနဲ့ HTTP error တွေ ဖမ်းမိမယ်လို့ မထင်ပါနဲ့။
3. လေ့ကျင့်ဖို့ key အစစ်ကို code ထဲ ထည့်တာ
Tutorial ကြည့်ရင်း ကိုယ့်ရဲ့ key အစစ်ကို file ထဲ ရိုက်ထည့်ပြီး git ထဲ commit လုပ်မိတာမျိုး ဖြစ်တတ်ပါတယ်။ ဒါကြောင့် လေ့ကျင့်တဲ့အဆင့်မှာ mock server ကို သုံးပါ၊ key အစစ်တွေကို နောက် lesson တွေမှာ ပြမယ့် environment variable နည်းနဲ့ပဲ ကိုင်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Course အတွက် folder တစ်ခု သီးသန့် ဖွင့်ပြီး sample တစ်ခုစီကို file တစ်ခုစီအဖြစ် သိမ်းပါ။
- Request ပို့တိုင်း မျှော်လင့်တဲ့ status ကို code မ run ခင် ကိုယ့်ဘာသာ ကြိုခန့်မှန်းကြည့်ပါ။
- Error message ကို အပြည့်အစုံ ဖတ်ပါ၊ ပထမစာကြောင်းကပဲ အရေးကြီးတာ မဟုတ်ဘဲ cause နဲ့ status တွေပါ ပါလေ့ရှိပါတယ်။
Mini challenge
Mini challenge: mock server ကို နှစ်ကြိမ် ping လုပ်ပါ
Route နှစ်ခုပါတဲ့ mock server တစ်ခုရေးပြီး client က နှစ်ခုလုံးကို ခေါ်ကာ status ကို print ထုတ်ပါ။ Body ကို res.ok ဖြစ်မှသာ parse လုပ်ပါ။
- GET /api/ping ကို status 200 နဲ့ JSON { pong: true } ပြန်ရမယ်
- တခြား path အားလုံးကို status 404 နဲ့ JSON { error: "not_found" } ပြန်ရမယ်
- Client က /api/ping နဲ့ /api/missing ကို ခေါ်ပြီး "<path> -> <status>" ပုံစံ print ထုတ်ရမယ်
- res.ok မဟုတ်ရင် JSON parse မလုပ်ရ၊ ပြီးရင် server ကို ပိတ်ရမယ်
import http from "node:http";
const server = http.createServer((req, res) => {
// TODO: GET /api/ping -> 200 and { pong: true }
// TODO: anything else -> 404 and { error: "not_found" }
});
// TODO: listen on port 0 and build the base URL
// TODO: loop over the two paths, call fetch, print "<path> -> <status>"
// TODO: only call res.json() when res.ok is true
// TODO: close the serverရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: /api/ping -> 200 {"pong":true} /api/missing -> 404 (not ok, body not parsed)
အရိပ်အမြွက် ကြည့်ရန်
Server ထဲမှာ အဖြေကို ternary နဲ့ ရွေးလို့ရပါတယ် (known ? 200 : 404)။ res.json() ကို if (res.ok) block ထဲမှာပဲ ခေါ်ပါ။ Server ကို ပိတ်ဖို့ မမေ့ပါနဲ့။
အဖြေ ကြည့်ရန်
import http from "node:http";
const server = http.createServer((req, res) => {
const known = req.method === "GET" && req.url === "/api/ping";
res.writeHead(known ? 200 : 404, { "Content-Type": "application/json" });
res.end(JSON.stringify(known ? { pong: true } : { error: "not_found" }));
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = "http://127.0.0.1:" + server.address().port;
for (const path of ["/api/ping", "/api/missing"]) {
const res = await fetch(base + path);
if (res.ok) {
console.log(path, "->", res.status, JSON.stringify(await res.json()));
} else {
console.log(path, "->", res.status, "(not ok, body not parsed)");
}
}
server.close();Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Web API ဆိုတာ client နဲ့ server ကြား HTTP နဲ့ ပြောတဲ့ contract ပါ။ Request မှာ method, URL, header, body ပါပြီး response မှာ status, header, body ပါပါတယ်။
- Response ကို အစဉ်လိုက် ဖတ်ပါ၊ status ကို အရင်၊ body ကို နောက်ဆုံး။ 200 တစ်ခုတည်းက ဘာမှ မသက်သေပြပါဘူး။
- Course က chapter ငါးခု၊ lesson 33 ခု (Basic 5, Intermediate 9, Advanced 14, Projects 3, Exercises 2) ရှိပါတယ်။
- Sample တွေက local mock server (port 0) နဲ့ fetch ကို သုံးလို့ offline၊ key မပါဘဲ output အတူတူ ထွက်ပါတယ်။
RFC 9110 — HTTP Semantics — IETF