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

Node.js HOME

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

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

  • Node.js ကို language တစ်ခု ဒါမှမဟုတ် framework တစ်ခုအဖြစ် မဟုတ်ဘဲ runtime (V8 + system API) အဖြစ် ရှင်းပြနိုင်ဖို့
  • Course ရဲ့ chapter ငါးခုနဲ့ chapter တစ်ခုစီက ဘာဆီ ဦးတည်နေလဲဆိုတာ ပြောနိုင်ဖို့
  • node command နဲ့ .js file တစ်ခုကို run ပြီး output ကို ဖတ်နိုင်ဖို့
  • Node မှာ မရှိတဲ့ browser API (window, document) နဲ့ Node က ထပ်ပေးတဲ့ API (process, fs) တွေကို ခွဲခြားနိုင်ဖို့

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

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

JavaScript က browser ထဲမှာ မွေးဖွားလာတဲ့ language ပါ။ Chrome ထဲမှာ JavaScript ကို run ပေးတာက V8 လို့ခေါ်တဲ့ engine တစ်ခုပါ။ Node.js က အဲဒီ engine ကို browser ထဲကနေ ထုတ်ယူပြီး ကွန်ပျူတာပေါ်က program တစ်ခု လိုအပ်တဲ့ အရာတွေ — file ဖတ်တာ၊ network ကနေ request လက်ခံတာ၊ တခြား program တွေ run တာ — ကို ထပ်ဖြည့်ပေးထားပါတယ်။

ဥပမာ ပြောရရင် engine တစ်လုံးတည်းကို ကားအမျိုးအစား မတူတာ နှစ်စီးမှာ တပ်ထားသလိုပါပဲ။ Browser ကားက web page တွေကို ပြဖို့ ဆောက်ထားပြီး Node ကားကတော့ server, command-line tool နဲ့ automation script တွေအတွက် ဆောက်ထားတာပါ။

Problem → Why → Solution

ပြဿနာ: browser JavaScript က ကွန်ပျူတာကို မထိနိုင်ဘူး

JavaScript နည်းနည်း တတ်ပြီးပြီ ဆိုပါစို့။ အခု ဓာတ်ပုံ 300 ကို နာမည်ပြောင်းချင်တယ်၊ ဒါမှမဟုတ် mobile app အတွက် API တစ်ခု ဆောက်ချင်တယ်။ Browser ထဲက JavaScript ကတော့ သင့် disk ပေါ်က file တွေကို ဖတ်ခွင့်မရှိသလို port တစ်ခုပေါ်မှာ request လာမယ့်အထိ စောင့်နေခွင့်လည်း မရှိပါဘူး — web site တိုင်းက သင့်ကွန်ပျူတာကို ထိနိုင်ရင် အန္တရာယ်ရှိလို့ပါ။

ဆန့်ကျင်ဘက်အနေနဲ့ browser မှာ ရေးနေကျ code ကို Node ထဲ ကူးထည့်လိုက်ရင်လည်း ချက်ချင်း error တက်ပါတယ်။ Node မှာ page မရှိတော့ document လည်း မရှိပါဘူး။

browser-habit.js
// Written for a browser tab, then run with: node browser-habit.js
const button = document.querySelector("#save");
button.addEventListener("click", () => alert("Saved!"));

// ReferenceError: document is not defined

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

JavaScript language ကိုယ်တိုင်က အတူတူပါပဲ။ ကွာတာက language ကို run ပေးတဲ့ host (runtime) က ထည့်ပေးတဲ့ object တွေပါ။ Browser က document, window, alert ကို ပေးပြီး Node က process, fs, http ကို ပေးပါတယ်။ ဒီ course တစ်ခုလုံးက Node ပေးတဲ့ အပိုင်းကို သင်တာပါ။

Core concept: Node.js က runtime တစ်ခုပါ

Node.js ဟာ language အသစ် မဟုတ်သလို Express လို framework လည်း မဟုတ်ပါဘူး။ JavaScript ကို ကွန်ပျူတာပေါ်မှာ run ပေးတဲ့ runtime တစ်ခုပါ။ အပိုင်းသုံးပိုင်း ပေါင်းထားတာလို့ မြင်ကြည့်ပါ — code ကို execute လုပ်ပေးတဲ့ V8 engine၊ file နဲ့ network အလုပ်တွေကို operating system ဆီ လွှဲပေးပြီး ပြီးတဲ့အခါ သင့် code ကို ပြန်ခေါ်ပေးတဲ့ event loop၊ နောက်ပြီး fs, path, http, crypto လို built-in module တွေပါ။

Built-in module တွေကို import { readFile } from "node:fs/promises" လို node: prefix နဲ့ ခေါ်သုံးပါတယ်။ prefix က "ဒါ npm ကနေ install လုပ်ထားတဲ့ package မဟုတ်ဘူး၊ Node ထဲမှာ ပါပြီးသား module ပါ" လို့ ရှင်းရှင်းလင်းလင်း ပြောပေးပါတယ်။ process ကတော့ import မလိုဘဲ အမြဲ ရှိနေတဲ့ global object ပါ — Node version, command-line argument နဲ့ environment variable တွေကို အဲဒီကနေ ဖတ်ရပါတယ်။

ဒီ course က Node.js 22 ဒါမှမဟုတ် ပိုသစ်တဲ့ LTS (Long-Term Support) version ကို ရည်ရွယ်ပါတယ်။ LTS version တွေက နှစ်ပေါင်းများစွာ bug fix နဲ့ security fix ရတဲ့ version တွေမို့ project အစစ်တွေမှာ အဲဒါကိုပဲ သုံးကြပါတယ်။ လက်ရှိ LTS ဘယ်ဟာလဲဆိုတာ nodejs.org မှာ စစ်ကြည့်ပါ။

Course ကို chapter ငါးခု ခွဲထားပါတယ်။ Basic က runtime ကို နားလည်ပြီး file တစ်ခု run နိုင်တဲ့အထိ၊ Intermediate က Node ရဲ့ core module တွေနဲ့ Express၊ Advanced က production မှာ လိုအပ်တဲ့ configuration, testing, security နဲ့ shutdown တွေ၊ Projects က REST API တစ်ခုကို အဆင့်ဆင့် ဆောက်တာ၊ Exercises က အားလုံးကို ပေါင်းပြီး လေ့ကျင့်တာပါ။

ChapterLessonဘာတွေ သင်မလဲ
Basic6Node ဆိုတာဘာလဲ၊ setup၊ first app၊ CommonJS module၊ ES module နဲ့ package.json
Intermediate9fs, path, http, npm, Express (routing, static file), Promise နဲ့ async/await, event နဲ့ stream
Advanced6Configuration, test runner, HTTP security, worker thread, logging, graceful shutdown
Projects5REST API တစ်ခု: GET all, GET one, POST နဲ့ User API mini project
Exercises2Core module တွေနဲ့ Express CRUD API ကို ပေါင်းလေ့ကျင့်ခန်း
မှတ်ထားရမယ့် ပုံစံ: file တစ်ခုက terminal အထိ
  1. app.js ကို ရေးတယ်
  2. node app.js က runtime ကို စတယ်
  3. V8 က code ကို run တယ်
  4. Node API တွေက disk/network ကို ထိတယ်
  5. Output က terminal မှာ ပေါ်တယ်

Language က browser နဲ့ အတူတူပါ၊ ကွာတာက ဘေးကနေ ထည့်ပေးတဲ့ API တွေပါ။

အထွေထွေ ပုံစံ: file တစ်ခုကို run တာ

file-name.js
// file-name.js
console.log(value);

// In a terminal, inside the folder that holds the file:
//   node file-name.js

Code ကို .js file တစ်ခုထဲ save လုပ်ပြီး အဲဒီ file ရှိတဲ့ folder ထဲကနေ node file-name.js လို့ run ပါတယ်။ console.log() က print လုပ်တာတွေ terminal ထဲမှာ ပေါ်လာပြီး file အဆုံးရောက်လို့ စောင့်စရာ အလုပ် မကျန်တော့ရင် process က သူ့ဘာသာ ပြီးသွားပါတယ်။

Example: ပထမဆုံး script — course map

ဒီ script က Node.js ထဲမှာ run နေကြောင်း နှုတ်ဆက်ပြီး chapter တစ်ခုချင်းစီရဲ့ lesson အရေအတွက်နဲ့ စုစုပေါင်းကို ပြပါတယ်။ hello.js အဖြစ် save လုပ်ပြီး node hello.js နဲ့ run ကြည့်ပါ။ နောက်ဆုံး line က browser ထဲမှာ ရှိတဲ့ window object ကို Node မှာ ရှာမတွေ့ကြောင်း ပြပါတယ်။

hello.js
// hello.js - run it with: node hello.js
const chapters = [
  ["Basic", 6],
  ["Intermediate", 9],
  ["Advanced", 6],
  ["Projects", 5],
  ["Exercises", 2],
];

console.log("Hello from Node.js!");
let total = 0;
for (const [name, lessonCount] of chapters) {
  console.log(`${name.padEnd(13)}${lessonCount} lessons`);
  total += lessonCount;
}
console.log(`${"Total".padEnd(13)}${total} lessons`);
console.log("Running inside a browser?", typeof window !== "undefined");

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

  1. Line 2const chapters = [

    Chapter တစ်ခုစီကို [name, lessonCount] ဆိုတဲ့ array လေးတစ်ခုအဖြစ် သိမ်းထားပါတယ်။ ဒါက browser မှာ ရေးနေကျ ပုံမှန် JavaScript ပဲ ဖြစ်ပြီး Node အတွက် ထူးခြားတာ ဘာမှ မလိုပါဘူး။

  2. Line 10console.log("Hello from Node.js!");

    Node မှာ console.log() က DevTools ထဲကို မဟုတ်ဘဲ process ရဲ့ standard output (stdout) ကို ရေးပါတယ်။ ဒါကြောင့် terminal ထဲမှာ ပေါ်လာတာပါ။

  3. Line 12for (const [name, lessonCount] of chapters) {

    for...of က chapter တစ်ခုချင်းကို ယူပြီး destructuring နဲ့ array လေးကို name နဲ့ lessonCount ဆိုပြီး ခွဲထုတ်ပါတယ်။

  4. Line 13name.padEnd(13)

    padEnd(13) က နာမည်ကို စာလုံး 13 လုံး ပြည့်အောင် space ဖြည့်ပေးလို့ lesson အရေအတွက်တွေ column တစ်ခုတည်းမှာ တန်းစီနေပါတယ်။

  5. Line 17typeof window !== "undefined"

    typeof ကို သုံးရင် မရှိတဲ့ variable ကို စစ်လည်း error မတက်ဘဲ "undefined" ပြန်ရပါတယ်။ Node မှာ window မရှိလို့ false ကို print ထုတ်ပါတယ်။

You should see
Hello from Node.js!
Basic        6 lessons
Intermediate 9 lessons
Advanced     6 lessons
Projects     5 lessons
Exercises    2 lessons
Total        28 lessons
Running inside a browser? false

Browser JavaScript နဲ့ Node.js ယှဉ်ကြည့်ရအောင်

အပိုင်းBrowserNode.js
Run နည်း<script> tag ဒါမှမဟုတ် DevTools ConsoleTerminal မှာ node file.js
Host က ပေးတဲ့ objectwindow, document, localStorageprocess, Buffer, node: module တွေ
File နဲ့ networkSandbox ထဲမှာ ကန့်သတ်ထားOS က ခွင့်ပြုသလောက် အပြည့်
နှစ်ဖက်လုံးမှာ ရှိတာconsole, setTimeout, fetch, URLconsole, setTimeout, fetch, URL

Lesson တစ်ခုချင်းစီမှာ အပိုင်းတွေ အစီအစဉ်တကျ ပါပါတယ် — ရိုးရိုးရှင်းပြချက်၊ ပြဿနာ၊ core concept၊ run လို့ရတဲ့ example၊ mini challenge နဲ့ quiz တွေပါ။ Feature တစ်ခု ဘာကြောင့် ရှိရတာလဲဆိုတာ ပြဿနာ အပိုင်းက ရှင်းပြလို့ code မကြည့်ခင် အဲဒါကို အရင်ဖတ်ပါ။ Example တွေကို ကူးမထည့်ဘဲ ကိုယ်တိုင် ရိုက်ကြည့်ပါ၊ ပြီးရင် တစ်ခုခု ပြောင်းပြီး output ဘယ်လို ပြောင်းသွားလဲ ကြည့်ပါ။ လိုအပ်တာက Node.js 22 ဒါမှမဟုတ် ပိုသစ်တဲ့ version၊ code editor တစ်ခု (VS Code လို) နဲ့ terminal တစ်ခုပဲ ဖြစ်ပါတယ်။

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

Real project တွေမှာ Node ဘယ်နေရာ ရှိလဲ

Node ရဲ့ အသိသာဆုံး အလုပ်က web backend ပါ — mobile app ဒါမှမဟုတ် React frontend က ခေါ်တဲ့ JSON API တွေကို Express လို framework တွေနဲ့ ဆောက်ကြပါတယ်။ Chat app နဲ့ live dashboard လို connection အများကြီးကို တစ်ပြိုင်နက် ဖွင့်ထားရတဲ့ real-time app တွေမှာလည်း Node ရဲ့ non-blocking I/O က အထူး သင့်တော်ပါတယ်။

Server မရေးတဲ့ frontend developer တွေကလည်း Node ကို နေ့တိုင်း သုံးနေကြပါတယ်။ npm၊ Vite၊ ESLint၊ Prettier နဲ့ TypeScript compiler တို့ဟာ Node ပေါ်မှာ run တဲ့ program တွေပါ။ Team တွေက database backup၊ report ထုတ်တာ၊ file တွေ ပြောင်းတာလို automation script တွေကိုလည်း Node နဲ့ ရေးကြပြီး cloud provider တွေရဲ့ serverless function (ဥပမာ AWS Lambda) တွေမှာလည်း Node runtime ကို ရွေးလို့ ရပါတယ်။ ဒါကြောင့် ဒီ course မှာ သင်ယူတာတွေက backend developer ဖြစ်ဖြစ် frontend developer ဖြစ်ဖြစ် အသုံးဝင်ပါတယ်။

အမှားများ

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

1. မှားတဲ့ folder ထဲကနေ run တာ

Terminal က Desktop မှာ ရှိနေပြီး hello.js က Documents/node-course ထဲမှာ ရှိရင် node hello.js က "Cannot find module '...hello.js'" (code MODULE_NOT_FOUND) error ပေးပါတယ်။ Node က terminal ရဲ့ လက်ရှိ folder ကနေ file ကို ရှာလို့ပါ။ cd နဲ့ file ရှိတဲ့ folder ထဲ အရင်ဝင်ပါ၊ ဒါမှမဟုတ် path အပြည့် ရေးပါ။

2. Node REPL ထဲမှာ node command ရိုက်တာ

node လို့ချည်း ရိုက်လိုက်ရင် > prompt ပါတဲ့ REPL (interactive mode) ထဲ ရောက်သွားပါတယ်။ အဲဒီထဲမှာ node hello.js လို့ ထပ်ရိုက်ရင် JavaScript code အဖြစ် ဖတ်လို့ SyntaxError တက်ပါတယ်။ Ctrl + C နှစ်ခါ နှိပ် (ဒါမှမဟုတ် .exit ရိုက်) ပြီး shell ထဲ ပြန်ထွက်ကာမှ run ပါ။

3. Browser-only API ကို Node မှာ သုံးတာ

alert(), document, localStorage တွေက browser ကပေးတဲ့ object တွေမို့ Node မှာ ReferenceError ဖြစ်ပါတယ်။ Terminal ကို ရေးချင်ရင် console.log()၊ data သိမ်းချင်ရင် file ဒါမှမဟုတ် database ကို သုံးပါ။

Mini challenge

Mini challenge: ဒီတစ်ပတ် study plan

ဒီတစ်ပတ် လေ့လာမယ့် lesson သုံးခုနဲ့ မိနစ်တွေကို print ထုတ်ပြီး စုစုပေါင်း အချိန်ကိုပါ တွက်ပေးတဲ့ study-plan.js ကို ရေးပါ။

  • Lesson တစ်ခုစီကို { lesson, minutes } object အဖြစ် array ထဲ သိမ်းရမယ်
  • Lesson တစ်ခုစီကို နံပါတ်တပ်ပြီး print ထုတ်ရမယ်
  • စုစုပေါင်း မိနစ်နဲ့ နာရီ (ဒသမ နှစ်နေရာ) ကို ပြရမယ်
  • process.versions.node ကို သုံးပြီး Node 22 ဒါမှမဟုတ် ပိုသစ်လား print ထုတ်ရမယ်
study-plan.js
// study-plan.js - run it with: node study-plan.js
const plan = [
  // TODO: three { lesson: "...", minutes: 0 } objects
];

// TODO: print a heading line

// TODO: loop over plan, print "1. <lesson> (<minutes> min)", add up minutes

// TODO: print "Total: <minutes> min = <hours> h" with 2 decimals

// TODO: read the major version from process.versions.node and print a check

ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ် (Node 22 ဒါမှမဟုတ် ပိုသစ်ရင် နောက်ဆုံး line က true ပါ): My Node.js plan for this week: 1. What is Node.js? (20 min) 2. Get Started (Setup) (25 min) 3. Your First App (30 min) Total: 75 min = 1.25 h Node.js 22 or newer: true

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

plan.forEach((item, index) => ...) က index ကို ပေးလို့ index + 1 နဲ့ နံပါတ်တပ်လို့ရပါတယ်။ process.versions.node က "22.20.0" လို string ဖြစ်လို့ split(".")[0] ကို Number() နဲ့ ပြောင်းပါ။

အဖြေ ကြည့်ရန်
study-plan.js
// study-plan.js - run it with: node study-plan.js
const plan = [
  { lesson: "What is Node.js?", minutes: 20 },
  { lesson: "Get Started (Setup)", minutes: 25 },
  { lesson: "Your First App", minutes: 30 },
];

console.log("My Node.js plan for this week:");
let totalMinutes = 0;
plan.forEach((item, index) => {
  console.log(`${index + 1}. ${item.lesson} (${item.minutes} min)`);
  totalMinutes += item.minutes;
});
console.log(`Total: ${totalMinutes} min = ${(totalMinutes / 60).toFixed(2)} h`);

const major = Number(process.versions.node.split(".")[0]);
console.log("Node.js 22 or newer:", major >= 22);

Quick Quiz 1 / 3

Node.js ထဲမှာ console.log(typeof window) ကို run ရင် ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

Node.js ကို အမှန်ကန်ဆုံး ဖော်ပြထားတာ ဘယ်ဟာလဲ။

Quick Quiz 3 / 3

node hello.js ကို run တော့ "Cannot find module '...hello.js'" ရပါတယ်။ အဖြစ်နိုင်ဆုံး အကြောင်းရင်းက ဘာလဲ။

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

  • Node.js က V8 engine၊ event loop နဲ့ built-in module တွေ ပေါင်းထားတဲ့ JavaScript runtime ပါ။
  • Language က browser နဲ့ တူပေမယ့် window/document မရှိဘဲ process, fs, http တွေ ရှိပါတယ်။
  • Built-in module တွေကို node: prefix နဲ့ import လုပ်ပြီး file တွေကို node file.js နဲ့ run ပါတယ်။
  • Course က Basic, Intermediate, Advanced, Projects, Exercises ဆိုပြီး lesson 28 ခု ရှိပါတယ်။

Introduction to Node.js — Node.js

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

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

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

Node.js HOME | Thuta Learning