ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
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 လည်း မရှိပါဘူး။
// 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 က အားလုံးကို ပေါင်းပြီး လေ့ကျင့်တာပါ။
| Chapter | Lesson | ဘာတွေ သင်မလဲ |
|---|---|---|
| Basic | 6 | Node ဆိုတာဘာလဲ၊ setup၊ first app၊ CommonJS module၊ ES module နဲ့ package.json |
| Intermediate | 9 | fs, path, http, npm, Express (routing, static file), Promise နဲ့ async/await, event နဲ့ stream |
| Advanced | 6 | Configuration, test runner, HTTP security, worker thread, logging, graceful shutdown |
| Projects | 5 | REST API တစ်ခု: GET all, GET one, POST နဲ့ User API mini project |
| Exercises | 2 | Core module တွေနဲ့ Express CRUD API ကို ပေါင်းလေ့ကျင့်ခန်း |
- app.js ကို ရေးတယ်
- node app.js က runtime ကို စတယ်
- V8 က code ကို run တယ်
- Node API တွေက disk/network ကို ထိတယ်
- Output က terminal မှာ ပေါ်တယ်
Language က browser နဲ့ အတူတူပါ၊ ကွာတာက ဘေးကနေ ထည့်ပေးတဲ့ API တွေပါ။
အထွေထွေ ပုံစံ: file တစ်ခုကို run တာ
// file-name.js
console.log(value);
// In a terminal, inside the folder that holds the file:
// node file-name.jsCode ကို .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 - 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 ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 2
const chapters = [Chapter တစ်ခုစီကို [name, lessonCount] ဆိုတဲ့ array လေးတစ်ခုအဖြစ် သိမ်းထားပါတယ်။ ဒါက browser မှာ ရေးနေကျ ပုံမှန် JavaScript ပဲ ဖြစ်ပြီး Node အတွက် ထူးခြားတာ ဘာမှ မလိုပါဘူး။
- Line 10
console.log("Hello from Node.js!");Node မှာ console.log() က DevTools ထဲကို မဟုတ်ဘဲ process ရဲ့ standard output (stdout) ကို ရေးပါတယ်။ ဒါကြောင့် terminal ထဲမှာ ပေါ်လာတာပါ။
- Line 12
for (const [name, lessonCount] of chapters) {for...of က chapter တစ်ခုချင်းကို ယူပြီး destructuring နဲ့ array လေးကို name နဲ့ lessonCount ဆိုပြီး ခွဲထုတ်ပါတယ်။
- Line 13
name.padEnd(13)padEnd(13) က နာမည်ကို စာလုံး 13 လုံး ပြည့်အောင် space ဖြည့်ပေးလို့ lesson အရေအတွက်တွေ column တစ်ခုတည်းမှာ တန်းစီနေပါတယ်။
- Line 17
typeof window !== "undefined"typeof ကို သုံးရင် မရှိတဲ့ variable ကို စစ်လည်း error မတက်ဘဲ "undefined" ပြန်ရပါတယ်။ Node မှာ window မရှိလို့ false ကို print ထုတ်ပါတယ်။
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? falseBrowser JavaScript နဲ့ Node.js ယှဉ်ကြည့်ရအောင်
| အပိုင်း | Browser | Node.js |
|---|---|---|
| Run နည်း | <script> tag ဒါမှမဟုတ် DevTools Console | Terminal မှာ node file.js |
| Host က ပေးတဲ့ object | window, document, localStorage | process, Buffer, node: module တွေ |
| File နဲ့ network | Sandbox ထဲမှာ ကန့်သတ်ထား | OS က ခွင့်ပြုသလောက် အပြည့် |
| နှစ်ဖက်လုံးမှာ ရှိတာ | console, setTimeout, fetch, URL | console, 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 - 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 - 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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- 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