Thuta Learning
Node.js
BasicWeb Developmentအခြေခံ

What is Node.js?

Node.jsသင်ခန်းစာ 220 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • V8၊ event loop နဲ့ built-in module တွေရဲ့ တာဝန်ကို ခွဲပြီး ရှင်းပြနိုင်ဖို့
  • Synchronous code၊ promise callback နဲ့ timer တွေ ဘယ်အစဉ်နဲ့ run မလဲ ခန့်မှန်းနိုင်ဖို့
  • Main thread ကို block လုပ်ရင် request တိုင်း ဘာကြောင့် နှေးသွားလဲ ရှင်းပြနိုင်ဖို့
  • Blocking (readFileSync) နဲ့ non-blocking (fs/promises) API ကို ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

စားသောက်ဆိုင်တစ်ဆိုင်မှာ စားပွဲထိုး တစ်ယောက်တည်း ရှိတယ်လို့ မြင်ကြည့်ပါ။ သူက order ယူပြီး မီးဖိုချောင်ကို ပေးလိုက်တယ်၊ ဟင်းကျက်တဲ့အထိ မီးဖိုရှေ့မှာ ရပ်မစောင့်ဘဲ နောက်စားပွဲကို သွားပါတယ်။ ဟင်းအဆင်သင့်ဖြစ်ရင် မီးဖိုချောင်က ခေါ်လို့ သွားယူပြီး ချပေးပါတယ်။

Node.js မှာ စားပွဲထိုးက သင့် JavaScript ကို run တဲ့ main thread ပါ၊ မီးဖိုချောင်က file နဲ့ network အလုပ်တွေ လုပ်ပေးတဲ့ operating system ပါ။ စားပွဲထိုးက ကိုယ်တိုင် ချက်ပြုတ်နေရင်တော့ (CPU အလုပ်ကြီး) စားပွဲအားလုံး စောင့်ရပါမယ်။

Problem → Why → Solution

ပြဿနာ: request တစ်ခုက ကျန်တဲ့ request အားလုံးကို စောင့်ခိုင်းတာ

အောက်က server က request တစ်ခုလာတိုင်း CSV file ကြီးတစ်ခုကို readFileSync နဲ့ ဖတ်ပြီး line တွေကို ရေတွက်ပါတယ်။ Local မှာ user တစ်ယောက်တည်း စမ်းတော့ မြန်နေပါတယ်။ User 100 ယောက် တစ်ပြိုင်နက် ဝင်လာတော့ request တစ်ခုစီက ရှေ့က request ပြီးတဲ့အထိ တန်းစီစောင့်ရပြီး ဘာမှမဆိုင်တဲ့ /health route တောင် ပြန်မဖြေနိုင်တော့ပါဘူး။

slow-server.mjs
import { createServer } from "node:http";
import { readFileSync } from "node:fs";

createServer((req, res) => {
  // Blocks the only JavaScript thread until the disk answers
  const report = readFileSync("big-report.csv", "utf8");
  // Blocks it again while counting characters one by one
  let lines = 0;
  for (const ch of report) if (ch === "\n") lines++;
  res.end(`lines: ${lines}`);
}).listen(3000);

ဘာကြောင့် နှေးသွားတာလဲ

Node က သင့် JavaScript ကို main thread တစ်ခုတည်းပေါ်မှာ run ပါတယ်။ အဲဒီ thread က readFileSync ဒါမှမဟုတ် loop ကြီးထဲမှာ ပိတ်မိနေသရွေ့ request အသစ်လက်ခံတာ၊ timer run တာ၊ တခြား callback တွေ ခေါ်တာ ဘာမှ မလုပ်နိုင်ပါဘူး။ Non-blocking API ကို သုံးရင် disk ကို စောင့်နေတဲ့ အချိန်မှာ thread က လွတ်နေပါတယ်။

Core concept: V8၊ event loop နဲ့ non-blocking I/O

Node ထဲမှာ အလုပ်ခွဲထားပါတယ်။ V8 က JavaScript ကို machine code အဖြစ် compile လုပ်ပြီး call stack ပေါ်မှာ run ပါတယ် — function တစ်ခု ခေါ်ရင် stack ပေါ်တင်၊ ပြီးရင် ဖယ်ပါတယ်။ libuv လို့ခေါ်တဲ့ C library က event loop နဲ့ I/O ကို ကိုင်ပါတယ်။ Network socket တွေအတွက် operating system ရဲ့ asynchronous notification ကို သုံးပြီး file system၊ crypto နဲ့ zlib လို အလုပ်တချို့အတွက်တော့ နောက်ကွယ်က thread pool လေးကို သုံးပါတယ်။

readFile() ကို ခေါ်လိုက်ရင် Node က ဖတ်တဲ့ အလုပ်ကို စတင်ပြီး Promise တစ်ခုကို ချက်ချင်း ပြန်ပေးပါတယ်၊ သင့် code က ဆက် run နေပါတယ်။ File ဖတ်ပြီးသွားရင် ရလဒ်ကို queue ထဲ ထည့်ထားပြီး call stack ဗလာဖြစ်တဲ့အခါ event loop က သင့် callback ကို ခေါ်ပေးပါတယ်။ ဒါကို non-blocking I/O လို့ ခေါ်ပါတယ်။ JavaScript run တာက thread တစ်ခုတည်း ဖြစ်ပေမယ့် Node တစ်ခုလုံးကတော့ နောက်ကွယ်မှာ thread တွေ သုံးနေပါတယ်။

အစဉ်ကိုလည်း မှတ်ထားပါ။ ပထမ — လက်ရှိ synchronous code အားလုံး ပြီးအောင် run ပါတယ်။ ဒုတိယ — microtask တွေ (Promise ရဲ့ .then callback နဲ့ await ပြီးနောက် ဆက်လာတဲ့ code၊ process.nextTick callback) ကို run ပါတယ်။ တတိယ — event loop က အဆင်သင့်ဖြစ်နေတဲ့ timer နဲ့ I/O callback တွေကို တစ်ခုချင်း ခေါ်ပြီး callback တစ်ခုပြီးတိုင်း microtask တွေကို ထပ်ရှင်းပါတယ်။

ဒါကြောင့် setTimeout(fn, 0) ဆိုတာ "ချက်ချင်း" မဟုတ်ဘဲ "လက်ရှိအလုပ် ပြီးတာနဲ့ အမြန်ဆုံး" လို့ အဓိပ္ပာယ်ရပါတယ်။ CPU ကို အကြာကြီး သုံးတဲ့ အလုပ် (ပုံကြီးတွေ resize လုပ်တာ၊ data အများကြီး တွက်တာ) ကတော့ event loop ကို ပိတ်ထားလို့ worker thread ဆီ ပို့ရပါမယ် — အဲဒါကို Advanced chapter မှာ သင်ပါမယ်။

Node ရဲ့ API တွေကို နာမည်ကြည့်ပြီး blocking ဖြစ်မဖြစ် သိနိုင်ပါတယ်။ fs module မှာ file ဖတ်နည်း သုံးမျိုး ရှိပါတယ် — Sync နဲ့ ဆုံးတဲ့ readFileSync က thread ကို ပိတ်ထားပြီး ရလဒ်ကို တိုက်ရိုက် ပြန်ပေးပါတယ်၊ callback style readFile က ရလဒ်ကို သင့် function ဆီ နောက်မှ ပို့ပေးပါတယ်၊ node:fs/promises က readFile ကတော့ Promise ပြန်ပေးလို့ await နဲ့ သုံးလို့ရပါတယ်။ ဒီ course မှာ promise style ကို အဓိကထား သုံးပါမယ်။

StyleဥပမာThread ကို block လုပ်လား
SynchronousreadFileSync(path, "utf8")လုပ်ပါတယ် — ပြီးတဲ့အထိ ဘာမှ မ run
CallbackreadFile(path, "utf8", (err, text) => {})မလုပ်ပါ
Promiseawait readFile(path, "utf8")မလုပ်ပါ
text
One main thread, work handed off to the OS
------------------------------------------
  your JavaScript (main thread)          operating system / libuv
  +-----------------------------+        +------------------------+
  | readFile("a.txt")  --------------->  | read a.txt (background)|
  | console.log("next line")    |        |                        |
  | ...stack becomes empty...   |        |        ...done         |
  +-----------------------------+        +-----------+------------+
                ^                                    |
                |        event loop: "a.txt is ready"|
                +------------ callback queue <-------+
မှတ်ထားရမယ့် ပုံစံ: run တဲ့ အစဉ်
  1. Synchronous code အားလုံး run
  2. I/O စပြီး callback မှတ်ထား
  3. Microtask (promise) တွေ run
  4. Event loop က ready callback ခေါ်

Call stack ဗလာ မဖြစ်မချင်း ဘယ် callback မှ မ run ပါဘူး။

အထွေထွေ ပုံစံ: non-blocking call

javascript
import { operation } from "node:module/promises";

operation(args).then((result) => {
  // runs later, after the OS has finished the work
});

// code here runs immediately, without waiting

Non-blocking function က အလုပ်ကို စတင်ပြီး Promise ကို ချက်ချင်း ပြန်ပေးပါတယ်။ .then() (ဒါမှမဟုတ် await) ထဲက code က ရလဒ်ရမှ run ပြီး function ခေါ်တဲ့ line ရဲ့ အောက်က code ကတော့ မစောင့်ဘဲ ဆက် run ပါတယ်။

Example: Node ကို browser နဲ့ ခွဲပြီး disk ကို မစောင့်တာ ကြည့်မယ်

ဒီ script က host object နှစ်ခုကို အရင်စစ်ပြီး ကိုယ့် file ကိုယ် ဖတ်ခိုင်းပါတယ်။ Output မှာ 3 က 4 ရဲ့ ရှေ့မှာ ပေါ်တာကို သတိထားကြည့်ပါ — ဖတ်တဲ့အလုပ် မပြီးခင် script ရဲ့ နောက်ဆုံး line ကို ရောက်နေပြီ ဖြစ်လို့ပါ။

intro.mjs
// intro.mjs - run it with: node intro.mjs
import { readFile } from "node:fs/promises";

console.log("1. Is there a window object?", typeof window !== "undefined");
console.log("2. Is there a process object?", typeof process !== "undefined");

// Ask the OS to read this very file. Node starts the read and moves on.
readFile(new URL(import.meta.url), "utf8").then((source) => {
  console.log("4. Read finished, first line:", source.split("\n")[0]);
});

console.log("3. Last line of the script (nothing waited for the disk)");

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 2import { readFile } from "node:fs/promises";

    node:fs/promises က file function တွေရဲ့ Promise ပြန်ပေးတဲ့ version ပါ။ node: prefix က Node ထဲမှာ ပါပြီးသား module ဖြစ်ကြောင်း ပြပါတယ်။

  2. Lines 4-5typeof process !== "undefined"

    Browser မှာဆို window က true၊ process က false ဖြစ်ပါတယ်။ Node မှာ ပြောင်းပြန်ပါ — ဒါက code ဘယ် runtime ထဲမှာ run နေလဲ သိဖို့ နည်းလမ်း တစ်ခုပါ။

  3. Line 8readFile(new URL(import.meta.url), "utf8").then((source) => {

    import.meta.url က ဒီ module ရဲ့ file:// URL ပါ။ readFile က ဖတ်တဲ့အလုပ်ကို OS ဆီ ပေးပြီး Promise ကို ချက်ချင်း ပြန်ပါတယ်၊ .then ထဲက code က နောက်မှ run ပါတယ်။

  4. Line 12console.log("3. Last line of the script (nothing waited for the disk)");

    ဒီ line က synchronous ဖြစ်လို့ file ဖတ်တာ ပြီးမပြီး မစောင့်ဘဲ ချက်ချင်း run ပါတယ်။ ဒါကြောင့် "3." က "4." ရဲ့ ရှေ့မှာ အမြဲ ပေါ်ပါတယ်။

You should see
1. Is there a window object? false
2. Is there a process object? true
3. Last line of the script (nothing waited for the disk)
4. Read finished, first line: // intro.mjs - run it with: node intro.mjs

Microtask နဲ့ timer: ဘယ်ဟာ အရင်လဲ

Promise callback တွေက microtask queue ထဲ ဝင်ပြီး timer တွေကတော့ event loop ရဲ့ timer phase ကို စောင့်ရပါတယ်။ ဒါကြောင့် delay 0 ပေးထားတဲ့ timer တောင် Promise callback ရဲ့ နောက်မှ run ပါတယ်။ အောက်က snippet ကို run ကြည့်ပြီး ခန့်မှန်းချက်နဲ့ တိုက်ကြည့်ပါ။ Line တွေကို C, B, A အစဉ်နဲ့ ရေးထားပေမယ့် A, B, C အစဉ်နဲ့ print ထွက်တာကို သတိထားပါ — schedule လုပ်တဲ့ call တွေကို source ထဲမှာ ရေးထားတဲ့ အစဉ်က သူတို့ရဲ့ callback ဘယ်အချိန် run မလဲဆိုတာကို သိပ်မပြောပါဘူး။

queue-order.mjs
setTimeout(() => console.log("C. timer callback (0 ms)"), 0);
Promise.resolve().then(() => console.log("B. promise callback"));
console.log("A. synchronous code");

setTimeout နဲ့ setImmediate

Main script ထဲမှာ setTimeout(fn, 0) နဲ့ setImmediate(fn) ကို ယှဉ်ရင် ဘယ်ဟာ အရင် run မလဲဆိုတာ စက်ရဲ့ အခြေအနေပေါ် မူတည်ပြီး ပြောင်းနိုင်ပါတယ်။ အဲဒီ အစဉ်ကို အားကိုးတဲ့ code မရေးပါနဲ့။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဒါက ဘာကြောင့် အရေးကြီးလဲ

API server တစ်ခုရဲ့ အချိန်အများစုက database၊ တခြား service ဒါမှမဟုတ် disk ကို စောင့်နေတာပါ။ Non-blocking I/O ကြောင့် Node process တစ်ခုတည်းက အဲဒီ စောင့်နေတဲ့ request ထောင်ချီကို တစ်ပြိုင်နက် ကိုင်ထားနိုင်ပါတယ်၊ request တစ်ခုအတွက် thread တစ်ခု ဖွင့်စရာ မလိုပါဘူး။ ဒါကြောင့် chat server၊ notification service နဲ့ API gateway လို I/O များတဲ့ system တွေမှာ Node ကို အသုံးများကြပါတယ်။

တစ်ဖက်မှာတော့ production မှာ ဖြစ်တတ်တဲ့ "server က ရုတ်တရက် နှေးသွားတယ်" ပြဿနာ အများစုက event loop ကို block လုပ်တဲ့ code ကနေ လာပါတယ် — request handler ထဲက readFileSync၊ JSON.parse လုပ်ရတဲ့ megabyte ရာချီ payload၊ ဒါမှမဟုတ် ဆိုးဆိုးရွားရွား backtrack လုပ်တဲ့ regular expression တွေပါ။ Monitoring tool တွေက "event loop lag" (callback တစ်ခု run ဖို့ ဘယ်လောက် ကြာကြာ စောင့်ရလဲ) ကို တိုင်းပေးလို့ ဒီလို ပြဿနာကို စောစော ရှာတွေ့နိုင်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Async function ရဲ့ ရလဒ်ကို ချက်ချင်း ရမယ်ထင်တာ

const text = readFile("a.txt", "utf8"); console.log(text); က file ထဲက စာကို မပြဘဲ Promise { <pending> } ကို ပြပါတယ်။ readFile က ရလဒ်ကို မဟုတ်ဘဲ နောက်မှ ရလာမယ့် ကတိ (Promise) ကို ပြန်ပေးလို့ပါ။ await ဒါမှမဟုတ် .then() ကို သုံးပါ။

2. setTimeout(fn, 0) က ချက်ချင်း run မယ်ထင်တာ

Delay 0 ဆိုတာ အနည်းဆုံး စောင့်ချိန်ပဲ ဖြစ်ပါတယ်။ Synchronous code နဲ့ microtask တွေ ပြီးမှ run ပြီး main thread က loop ကြီးထဲမှာ ပိတ်မိနေရင် အဲဒီ loop ပြီးတဲ့အထိ ထပ်စောင့်ရပါတယ်။

3. Request handler ထဲမှာ Sync API သုံးတာ

readFileSync၊ execSync လို function တွေက startup မှာ config ဖတ်တာလို တစ်ကြိမ်တည်း အလုပ်တွေအတွက် ပြဿနာ မရှိပါဘူး။ Request တိုင်း ခေါ်ရင်တော့ user တိုင်းက တစ်ယောက်ကို တစ်ယောက် တန်းစီစောင့်ရပါတယ်။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Server code ထဲမှာ node:fs/promises လို promise-based API ကို default အဖြစ် သုံးပြီး Sync version တွေကို startup script နဲ့ CLI tool အသေးလေးတွေအတွက်ပဲ ချန်ထားပါ။
  • Callback တစ်ခုထဲမှာ အလုပ်များများ မလုပ်ပါနဲ့၊ item ထောင်ချီ loop ပတ်ရမယ်ဆိုရင် အပိုင်းလိုက် ခွဲလုပ်တာ ဒါမှမဟုတ် worker thread ဆီ ပို့တာကို စဉ်းစားပါ။
  • Code အစဉ်ကို ယုံကြည်ချင်ရင် await နဲ့ ရေးပါ၊ timer delay တွေကို အစဉ်ထိန်းဖို့ နည်းလမ်းအဖြစ် မသုံးပါနဲ့။
  • Code အသစ်ရေးတဲ့အခါ အဲဒီ function က I/O ကို စောင့်နေတာလား CPU ကို သုံးနေတာလား အမြဲ မေးကြည့်ပါ၊ အဖြေက ဘယ် API ရွေးရမလဲ ပြောပေးပါတယ်။

Mini challenge

Mini challenge: run မလုပ်ခင် အစဉ်ကို ခန့်မှန်းပါ

Synchronous log နှစ်ခု၊ timer နှစ်ခု၊ promise callback တစ်ခုနဲ့ 200 ms ကြာတဲ့ busy loop တစ်ခု ပါတဲ့ script ရေးပါ။ Run မလုပ်ခင် output အစဉ်ကို ချရေးထားပြီးမှ run ပြီး တိုက်ကြည့်ပါ။

  • Delay 0 ms နဲ့ 20 ms ပေးထားတဲ့ setTimeout နှစ်ခု
  • Promise.resolve().then() callback တစ်ခု
  • Synchronous log နှစ်ခုကြားမှာ main thread ကို 200 ms block လုပ်တဲ့ loop
  • 0 ms timer က busy loop ကို စောင့်ခဲ့ရလား (true/false) print ထုတ်ရမယ်
order.mjs
// order.mjs - predict the order before you run it!
const start = Date.now();

// TODO: a 20 ms timer that logs its label
// TODO: a 0 ms timer that logs whether Date.now() - start >= 200
// TODO: a promise callback

// TODO: first synchronous log
// TODO: busy loop until 200 ms have passed (while + Date.now())
// TODO: second synchronous log

ရလဒ်: Output က ဒီအစဉ်အတိုင်း ပေါ်ရပါမယ်: 1. first synchronous log 2. second synchronous log, after 200 ms of blocking 3. promise callback (microtask) 4. 0 ms timer - did it wait for the busy loop? true 5. 20 ms timer

အရိပ်အမြွက် ကြည့်ရန်

const end = Date.now() + 200; while (Date.now() < end) {} က thread ကို 200 ms ပိတ်ထားပါတယ်။ Timer နှစ်ခုလုံး loop ပြီးချိန်မှာ သက်တမ်းကုန်ပြီးသား ဖြစ်နေလို့ ဘယ်ဟာ အရင်ကုန်လဲ စဉ်းစားပါ။

အဖြေ ကြည့်ရန်
order.mjs
// order.mjs - predict the order before you run it!
const start = Date.now();

setTimeout(() => console.log("5. 20 ms timer"), 20);
setTimeout(() => {
  const waited = Date.now() - start;
  console.log("4. 0 ms timer - did it wait for the busy loop?", waited >= 200);
}, 0);
Promise.resolve().then(() => console.log("3. promise callback (microtask)"));

console.log("1. first synchronous log");
const end = Date.now() + 200;
while (Date.now() < end) {
  // busy loop: the only JavaScript thread is stuck here for 200 ms
}
console.log("2. second synchronous log, after 200 ms of blocking");

Quick Quiz 1 / 3

setTimeout(() => console.log("T"), 0); Promise.resolve().then(() => console.log("P")); console.log("S"); — output အစဉ်က ဘာလဲ။

Quick Quiz 2 / 3

Request handler ထဲမှာ 2 စက္ကန့်ကြာတဲ့ CPU loop တစ်ခု ရှိတယ်။ Request 3 ခု တစ်ပြိုင်နက် ရောက်လာရင် ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

const data = readFile("notes.txt", "utf8"); console.log(data); (node:fs/promises ကနေ readFile) က ဘာ print ထုတ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • V8 က JavaScript ကို run ပြီး libuv ရဲ့ event loop က I/O ကို နောက်ကွယ်မှာ လုပ်ပေးပါတယ်။
  • JavaScript က main thread တစ်ခုတည်းပေါ်မှာ run လို့ အဲဒီ thread ကို block လုပ်ရင် အားလုံး စောင့်ရပါတယ်။
  • အစဉ်: synchronous code → microtask (promise) → timer နဲ့ I/O callback။
  • Request handler ထဲမှာ non-blocking API ကို သုံးပြီး CPU အလုပ်ကြီးတွေကို worker thread ဆီ ပို့ပါ။

Node.js event loop — Node.js

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

What is Node.js? | Thuta Learning