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

Practice: Core Modules (fs, path, http)

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

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

  • fs/promises နဲ့ file ကို ရေး၊ ထပ်ဖြည့်၊ ဖတ်ပြီး callback version နဲ့ ဘာကွာလဲ ရှင်းပြနိုင်ဖို့
  • path.join၊ basename၊ extname နဲ့ Windows/Linux နှစ်မျိုးလုံးမှာ အလုပ်လုပ်တဲ့ path တွေ တည်ဆောက်နိုင်ဖို့
  • http.createServer() နဲ့ method နဲ့ url ကို စစ်တဲ့ route တစ်ခုနဲ့ 404 ကို ရေးနိုင်ဖို့
  • Temp folder နဲ့ server ကို practice ပြီးတိုင်း ရှင်းလင်းပြီး ENOENT လို error code တွေကို ကိုင်တွယ်နိုင်ဖို့

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

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

ဒီ lesson မှာ အသစ်မသင်ပါဘူး။ fs၊ path၊ http lesson တွေမှာ ဖတ်ခဲ့တာတွေကို ကိုယ့်လက်နဲ့ ရေးကြည့်တဲ့ လေ့ကျင့်ခန်းပါ။ ကားမောင်းသင်တဲ့အခါ စာအုပ်ဖတ်တာနဲ့ လမ်းပေါ် တကယ်မောင်းတာ မတူသလိုပါပဲ။ Code ကို ကိုယ်တိုင် run ကြည့်မှ "file က ဘယ်မှာ ရောက်သွားတာလဲ"၊ "server က ဘာကြောင့် မပိတ်တာလဲ" ဆိုတဲ့ မေးခွန်းတွေ ပေါ်လာပြီး အဲဒီအဖြေတွေက တကယ် မှတ်မိစေပါတယ်။

Task သုံးခုကို တစ်ခုပြီးတစ်ခု လုပ်ပါမယ်။ File ရေးပြီး ပြန်ဖတ်တာ၊ path ကို လုံခြုံစွာ တည်ဆောက်တာ၊ request တွေကို ဖြေပေးတဲ့ server သေးသေးလေး ဆောက်တာ ဖြစ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: စက်တစ်လုံးမှာပဲ အလုပ်လုပ်တဲ့ script

အောက်က script က developer ရဲ့ Mac မှာ အဆင်ပြေပေမယ့် တခြားနေရာမှာ ပြဿနာ သုံးခု ပေါ်ပါတယ်။ 'data' + '/' + 'todo.txt' လို string ပေါင်းထားလို့ Windows ရဲ့ \ separator နဲ့ path တွေ ရောနေပါတယ်။ Relative path ဖြစ်လို့ file က script ရှိတဲ့ folder မှာ မဟုတ်ဘဲ node ကို run ခဲ့တဲ့ folder (current working directory) မှာ ရောက်သွားပြီး data folder မရှိရင် ENOENT error တက်ပါတယ်။

နောက်ဆုံးက port 4000 ပေါ်မှာ listen လုပ်ပြီး ဘယ်တော့မှ မပိတ်လို့ test တစ်ခုထဲမှာ run ရင် process က ဆက် run နေပြီး ဒုတိယအကြိမ် run ရင် EADDRINUSE နဲ့ ကျပါတယ်။

practice-draft.js
const fs = require('fs');
const http = require('http');

fs.writeFile('data' + '/' + 'todo.txt', 'Learn Node.js', () => {}); // error ignored

http.createServer((req, res) => {
  res.end(new Date().toISOString());          // every URL gets 200
}).listen(4000);                                // fixed port, never closed

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

Relative path တွေကို Node က process.cwd() နဲ့ ဖြေရှင်းပါတယ်၊ script ရဲ့ နေရာနဲ့ မဟုတ်ပါဘူး။ Callback ထဲမှာ err ကို မစစ်ရင် error က အသံတိတ် ပျောက်သွားပါတယ်။ Listen လုပ်နေတဲ့ server ကလည်း event loop ကို အလုပ်ရှိနေသေးတယ်လို့ ပြောနေလို့ process မထွက်ပါဘူး။

Core concept: task သုံးခုနဲ့ စည်းကမ်းသုံးခု

Task တစ်ခုစီမှာ module တစ်ခုကို အဓိကထား လေ့ကျင့်ပြီး စည်းကမ်းတစ်ခုစီကို မှတ်သားရပါမယ်။

Task 1 (fs): error ကို အမြဲ စစ်ပါ

fs မှာ API ပုံစံ သုံးမျိုး ရှိပါတယ်: callback (fs.writeFile(file, data, cb))၊ promise (require('node:fs/promises'))၊ sync (fs.writeFileSync)။ Callback မှာ ပထမ argument က err ဖြစ်ပြီး if (err) နဲ့ စစ်ရပါတယ်။ Promise version မှာတော့ await နဲ့ ရေးပြီး error ကို try/catch ဒါမှမဟုတ် .catch() နဲ့ ဖမ်းပါတယ်။ Write ပြီးမှ read လုပ်ရမယ့် အစဉ်လိုက် အလုပ်တွေအတွက် await က callback တွေ အထပ်ထပ် ချိတ်တာထက် ဖတ်ရလွယ်ပါတယ်။

Task 2 (path): separator ကို ကိုယ်တိုင် မရေးပါနဲ့

path.join('data', 'logs', 'app.log') က Linux/macOS မှာ data/logs/app.log၊ Windows မှာ data\logs\app.log ပြန်ပေးပါတယ်။ path.sep က လက်ရှိ OS ရဲ့ separator ပါ။ path.basename က နောက်ဆုံးအပိုင်း (app.log)၊ path.extname က extension (.log) ကို ပေးပါတယ်။ Script ရှိတဲ့ folder ကို အခြေခံချင်ရင် path.join(__dirname, 'data') ကို သုံးပါ။

Task 3 (http): ဖွင့်ထားတာ ပြန်ပိတ်ပါ

http.createServer() ရဲ့ handler က request တိုင်းမှာ run ပြီး req.method နဲ့ req.url ကို ကိုယ်တိုင် စစ်ရပါတယ်၊ Express လို router မရှိပါဘူး။ req.url ထဲမှာ query string ပါ ပါလို့ /time?tz=utc က '/time' နဲ့ မညီပါဘူး၊ query လိုရင် new URL(req.url, 'http://localhost') နဲ့ parse လုပ်ပါ။ Response တစ်ခုစီကို writeHead() နဲ့ status/header သတ်မှတ်ပြီး end() နဲ့ ပြီးဆုံးစေပါတယ်။ Practice script မှာ listen(0) နဲ့ port အလွတ်ယူပြီး စမ်းပြီးတာနဲ့ server.close() ခေါ်ပါ။ Temp folder ကိုလည်း fs.rm() နဲ့ ဖျက်ပါ။

callback-style.js
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');

const file = path.join(os.tmpdir(), 'callback-demo.txt');
fs.writeFile(file, 'written with a callback', 'utf8', (err) => {
  if (err) return console.error('Write failed:', err.message);
  fs.readFile(file, 'utf8', (err2, data) => {
    if (err2) return console.error('Read failed:', err2.message);
    console.log('Read back:', data);
    fs.unlink(file, () => {}); // tidy up
  });
});

အပေါ်က snippet က Task 1 ကို callback ပုံစံနဲ့ ရေးထားတာပါ။ Read က write ရဲ့ callback ထဲမှာ ရှိနေရလို့ အဆင့်တစ်ခု တိုးတိုင်း indentation တစ်ဆင့် ထပ်တိုးပါတယ်။ Example က အဲဒါကို await နဲ့ ပြန်ရေးထားပါတယ်။ ပုံစံနှစ်မျိုးလုံး မှန်ပြီး လက်တွေ့ code တွေထဲမှာ နှစ်မျိုးလုံး တွေ့ရမှာမို့ ဘယ်ပုံစံကိုမဆို ဖတ်တတ်ထားဖို့ တန်ပါတယ်။

Mental model: ဖွင့်၊ သုံး၊ ပိတ်
  1. Temp folder နဲ့ port 0 ကို ယူ
  2. path နဲ့ file path တည်ဆောက်
  3. await နဲ့ ရေး၊ ဖတ်၊ request ပို့
  4. Server ပိတ်ပြီး folder ဖျက်

ကိုယ်ဖွင့်ခဲ့တဲ့ resource (file၊ folder၊ server) တိုင်းကို ပိတ်ဖို့ ဘယ်နေရာမှာ ရေးထားလဲ ဆိုတာ ပြောနိုင်ရပါမယ်။

အခြေခံ ပုံစံ

javascript
const fs = require('node:fs/promises');
const path = require('node:path');
const http = require('node:http');

await fs.writeFile(path.join(dir, 'name.txt'), text, 'utf8');
const content = await fs.readFile(path.join(dir, 'name.txt'), 'utf8');

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/route') return send(res, 200, body);
  send(res, 404, 'Not Found');                  // send = writeHead() + end()
});

fs/promises ကို await နဲ့ သုံးပြီး path ကို path.join နဲ့ တည်ဆောက်ပါ။ http handler က method နဲ့ url ကို စစ်ပြီး မကိုက်ရင် 404 ပြန်ပါတယ်။

Task သုံးခုလုံး script တစ်ခုတည်းမှာ

Script က task သုံးခုကို async main() function တစ်ခုထဲမှာ အစဉ်လိုက် လုပ်ပါတယ်။ File ကို project folder ထဲ မရေးဘဲ OS ရဲ့ temp folder အောက်က folder အသစ်ထဲ ရေးလို့ ဘာမှ မညစ်ပတ်ပါဘူး။ /time ရဲ့ အချိန်က run တိုင်း ပြောင်းလို့ output မှာ အချိန်ကို မပြဘဲ မှန်ကန်တဲ့ date ဟုတ်/မဟုတ်ကိုပဲ ပြပါတယ်။

practice.js
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const http = require('node:http');

async function main() {
  // Task 1: write a file and read it back, inside a throwaway temp folder
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'practice-'));
  const file = path.join(dir, 'todo.txt');
  await fs.writeFile(file, 'Learn Node.js modules\n', 'utf8');
  await fs.appendFile(file, 'Practise fs, path and http\n', 'utf8');
  const text = await fs.readFile(file, 'utf8');
  console.log('Task 1 lines:', text.trim().split('\n'));

  // Task 2: build paths with the path module, never by gluing '/'
  const logPath = path.join('data', 'logs', 'app.log');
  console.log('Task 2 parts:', logPath.split(path.sep));
  console.log('  base:', path.basename(logPath), '| ext:', path.extname(logPath));

  // Task 3: a tiny http server with one route and a 404
  const server = http.createServer((req, res) => {
    if (req.method === 'GET' && req.url === '/time') {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ now: new Date().toISOString() }));
      return;
    }
    res.writeHead(404, { 'Content-Type': 'text/plain' });
    res.end('Not Found');
  });
  await new Promise((resolve) => server.listen(0, resolve));
  const base = `http://localhost:${server.address().port}`;

  const timeRes = await fetch(`${base}/time`);
  const { now } = await timeRes.json();
  const valid = !Number.isNaN(Date.parse(now));
  console.log('Task 3 /time:', timeRes.status, 'valid date:', valid);
  const missing = await fetch(`${base}/nope`);
  console.log('Task 3 /nope:', missing.status, await missing.text());

  server.close();
  await fs.rm(dir, { recursive: true, force: true });
}

main().catch((err) => {
  console.error('Practice failed:', err.message);
  process.exitCode = 1;
});

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

  1. Throwaway folderconst dir = await fs.mkdtemp(path.join(os.tmpdir(), 'practice-'));

    mkdtemp က prefix နောက်မှာ random စာလုံးတွေ ထည့်ပြီး folder အသစ် ဖန်တီးပါတယ်။ Run တိုင်း folder မတူလို့ run အဟောင်းရဲ့ file တွေနဲ့ မရောပါဘူး။

  2. Append, then readawait fs.appendFile(file, 'Practise fs, path and http\n', 'utf8');

    writeFile က file ကို အသစ်ရေး (ရှိပြီးသားကို ဖျက်ရေး) ပြီး appendFile က နောက်ဆုံးမှာ ထပ်ဖြည့်ပါတယ်။ ဒါကြောင့် line နှစ်ကြောင်း ပြန်ဖတ်ရပါတယ်။

  3. OS-neutral outputconsole.log('Task 2 parts:', logPath.split(path.sep));

    Path string ကို တိုက်ရိုက် print ရင် Windows နဲ့ Linux မှာ မတူပါဘူး။ path.sep နဲ့ ခွဲလိုက်ရင် OS တိုင်းမှာ ['data', 'logs', 'app.log'] အတူတူ ရပါတယ်။

  4. Manual routingif (req.method === 'GET' && req.url === '/time') {

    http module မှာ router မရှိလို့ method နဲ့ url ကို ကိုယ်တိုင် နှိုင်းယှဉ်ပါတယ်။ return က 404 code ဆီ မရောက်အောင် တားပါတယ်။

  5. Waiting for listenawait new Promise((resolve) => server.listen(0, resolve));

    listen() က callback ပုံစံ ဖြစ်လို့ Promise နဲ့ ထုပ်ပြီး await လုပ်ပါတယ်။ ဒါမှ server အဆင်သင့် ဖြစ်ပြီးမှ fetch ပို့ပါမယ်။

  6. Clean upawait fs.rm(dir, { recursive: true, force: true });

    server.close() နဲ့ fs.rm() က practice script က ဖွင့်ခဲ့တဲ့ အရာအားလုံးကို ပြန်ရှင်းပြီး process ကို သန့်သန့်ရှင်းရှင်း ထွက်စေပါတယ်။

You should see
Task 1 lines: [ 'Learn Node.js modules', 'Practise fs, path and http' ]
Task 2 parts: [ 'data', 'logs', 'app.log' ]
  base: app.log | ext: .log
Task 3 /time: 200 valid date: true
Task 3 /nope: 404 Not Found

ကိုယ့်ကိုယ်ကို စစ်ဆေးရန် နဲ့ ထပ်လေ့ကျင့်ရန်

Example ကို copy မကူးဘဲ task တစ်ခုချင်းစီကို ကိုယ်တိုင် အရင်ရေးကြည့်ပါ။ ပြီးရင် အောက်က table နဲ့ တိုက်စစ်ပါ။ ဘယ်ဘက်ကော်လံက "ဒါလုပ်နိုင်ရင် task ပြီးပြီ" ဆိုတဲ့ စစ်ချက်ဖြစ်ပြီး ညာဘက်ကော်လံက အချိန်ရှိရင် ထပ်စမ်းကြည့်ဖို့ အဆင့်မြင့် လေ့ကျင့်ခန်းပါ။

Taskပြီးပြီလို့ ဘယ်လိုသိမလဲထပ်စမ်းရန်
1. fsLine နှစ်ကြောင်းကို ပြန်ဖတ်နိုင်ပြီး folder ကို ဖျက်ပြီးသားFile မရှိရင် ENOENT ကို ဖမ်းပြီး message ပြပါ
2. pathOS တိုင်းမှာ output တူတူ ထွက်path.parse() ရဲ့ ရလဒ်ကို print ကြည့်ပါ
3. http/time က 200၊ ကျန်တာ 404၊ process ထွက်သွားPOST /time ကို 405 နဲ့ ငြင်းပါ

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

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

Express သုံးတဲ့ project တွေမှာတောင် ဒီ module သုံးခုကို နေ့တိုင်း တွေ့ရပါတယ်။ Upload လုပ်လာတဲ့ file တွေကို disk ပေါ် သိမ်းတာ၊ config file ဖတ်တာ၊ log file ရေးတာ၊ build script တွေမှာ folder တွေ ဖန်တီးတာက fs နဲ့ path ရဲ့ အလုပ်ပါ။ Express ကိုယ်တိုင်လည်း အတွင်းမှာ http module ပေါ်မှာ တည်ဆောက်ထားလို့ app.listen() က http server တစ်ခုကို ပြန်ပေးပါတယ်။

Test တွေမှာ ဒီ lesson ရဲ့ pattern ကို အမြဲ သုံးပါတယ်။ Test တစ်ခုစီက mkdtemp နဲ့ ကိုယ်ပိုင် folder ယူ၊ port 0 နဲ့ ကိုယ်ပိုင် server စ၊ ပြီးရင် ပြန်ရှင်းပါတယ်။ ဒါကြောင့် test တွေကို parallel run လည်း တစ်ခုနဲ့တစ်ခု မထိခိုက်ပါဘူး။ CI server ပေါ်မှာ Linux နဲ့ developer စက်ပေါ်မှာ Windows ဖြစ်နေရင်တောင် path module က code တစ်ခုတည်းကို နှစ်နေရာလုံးမှာ အလုပ်လုပ်စေပါတယ်။ server.close() မေ့တာကလည်း test suite တစ်ခုက အလုပ်တွေ ပြီးသွားပေမယ့် ဆက် hang နေရတဲ့ အဖြစ်အများဆုံး အကြောင်းရင်းတွေထဲက တစ်ခုပါ။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

Relative path က script folder လို့ ထင်တာ

fs.readFile('config.json') က node ကို run ခဲ့တဲ့ folder မှာ ရှာပါတယ်။ Project root ကနေ node src/app.js လို့ run ရင် src/config.json ကို ရှာမတွေ့ပါဘူး။ Script နဲ့ ဆက်စပ်တဲ့ file ဆိုရင် path.join(__dirname, 'config.json') ကို သုံးပါ။

res.end() ကို မေ့တာ

http handler ထဲမှာ writeHead() ပဲ ခေါ်ပြီး end() မခေါ်ရင် client က response ပြီးဆုံးမှာကို စောင့်နေပြီး browser က loading ပဲ ပြနေပါမယ်။ အောက်ဆုံးက 404 အပါအဝင် branch တိုင်းမှာ end() ခေါ်ထားလား စစ်ပါ။

Error code အားလုံးကို "file မရှိ" လို့ ယူဆတာ

catch ထဲမှာ ဘာ error ပဲဖြစ်ဖြစ် null ပြန်ရင် permission error (EACCES) လို တကယ့်ပြဿနာတွေကိုပါ ဖုံးသွားပါတယ်။ err.code === 'ENOENT' ဖြစ်မှ "မရှိဘူး" လို့ ကိုင်တွယ်ပြီး ကျန်တာကို throw ပြန်လုပ်ပါ။

Best Practices

ကောင်းမွန်တဲ့ အလေ့အကျင့်များ

  • Core module တွေကို require('node:fs') လို node: prefix နဲ့ ခေါ်ပါ၊ npm package နဲ့ နာမည်တူလည်း မရောတော့ပါဘူး
  • Practice file တွေကို os.tmpdir() အောက်မှာ ရေးပြီး project folder ကို သန့်ရှင်းအောင် ထားပါ
  • Fixed port အစား port 0 ကို သုံးပြီး server.address().port နဲ့ တကယ့် port ကို ဖတ်ပါ

Mini challenge

Notes server: Task 4 နဲ့ /hello route

File ဖတ်တဲ့ logic ကို readNoteSafe(file) function အဖြစ် ခွဲထုတ်ပါ (Task 4)။ ပြီးရင် http server မှာ GET /hello (JSON message) နဲ့ GET /notes/<name> (temp folder ထဲက <name>.txt ကို ဖတ်) route နှစ်ခု ရေးပါ။ /notes/..%2Fsecret လို folder ပြင်ပကို ထွက်ဖို့ ကြိုးစားတဲ့ request ကို ကာကွယ်ပါ။

  • readNoteSafe() က ENOENT ဆိုရင် null ပြန်ပြီး တခြား error ကို throw ပြန်လုပ်ပါ
  • /hello က { message: 'Hello Node.js' } ကို 200 နဲ့ ပြန်ပါ
  • /notes/<name> က decodeURIComponent ပြီး path.basename နဲ့ folder အပိုင်းတွေကို ဖယ်ပါ
  • Note မရှိရင် 404 JSON၊ ပြီးရင် server ကို ပိတ်ပြီး temp folder ကို ဖျက်ပါ
notes-server.js
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const http = require('node:http');

async function readNoteSafe(file) {
  // TODO: return the text; null on ENOENT; rethrow anything else
}

function sendJson(res, status, body) {
  // TODO: writeHead with Content-Type application/json, then end(JSON)
}

async function main() {
  // TODO: mkdtemp a folder and write todo.txt into it
  // TODO: createServer: /hello, /notes/<name> (basename!), else 404
  // TODO: listen(0), fetch four URLs, print status + body, close, rm folder
}

main();

ရလဒ်: /hello နဲ့ /notes/todo က 200 ("Buy rice")၊ /notes/shopping က 404၊ /notes/..%2Fsecret က "No note named secret" နဲ့ 404 ဖြစ်ပါမယ်။

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

req.url.slice('/notes/'.length) က name အပိုင်းကို ပေးပါတယ်။ decodeURIComponent('..%2Fsecret') က '../secret' ဖြစ်ပြီး path.basename('../secret') က 'secret' ဖြစ်လို့ folder ပြင်ပကို မထွက်နိုင်တော့ပါဘူး။

အဖြေ ကြည့်ရန်
notes-server.js
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const http = require('node:http');

// Returns the file's text, or null when it does not exist
async function readNoteSafe(file) {
  try {
    return await fs.readFile(file, 'utf8');
  } catch (err) {
    if (err.code === 'ENOENT') return null;
    throw err; // permission problems etc. are real errors
  }
}

function sendJson(res, status, body) {
  res.writeHead(status, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify(body));
}

async function main() {
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'notes-'));
  await fs.writeFile(path.join(dir, 'todo.txt'), 'Buy rice', 'utf8');

  const server = http.createServer(async (req, res) => {
    if (req.url === '/hello') return sendJson(res, 200, { message: 'Hello Node.js' });

    if (req.url.startsWith('/notes/')) {
      // basename() strips any folders, so "../secret" becomes "secret"
      const name = path.basename(decodeURIComponent(req.url.slice('/notes/'.length)));
      const text = await readNoteSafe(path.join(dir, `${name}.txt`));
      if (text === null) return sendJson(res, 404, { error: `No note named ${name}` });
      return sendJson(res, 200, { name, text });
    }
    sendJson(res, 404, { error: 'Not Found' });
  });

  await new Promise((resolve) => server.listen(0, resolve));
  const base = `http://localhost:${server.address().port}`;
  for (const url of ['/hello', '/notes/todo', '/notes/shopping', '/notes/..%2Fsecret']) {
    const res = await fetch(base + url);
    console.log(url, res.status, JSON.stringify(await res.json()));
  }
  server.close();
  await fs.rm(dir, { recursive: true, force: true });
}

main().catch((err) => {
  console.error('Failed:', err.message);
  process.exitCode = 1;
});

Quick Quiz 1 / 3

Project root ကနေ node scripts/report.js လို့ run ပါတယ်။ report.js ထဲက fs.readFile('data.csv') က ဘယ် file ကို ရှာမလဲ။

Quick Quiz 2 / 3

path.join('data', 'logs', 'app.log') ကို Windows ပေါ်မှာ run ရင် ဘာ ပြန်ရမလဲ။

Quick Quiz 3 / 3

Example script ထဲက server.close() လိုင်းကို ဖျက်လိုက်ရင် ဘာဖြစ်မလဲ။

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

  • fs/promises ကို await နဲ့ သုံးရင် write ပြီးမှ read လို အစဉ်လိုက် အလုပ်တွေ ဖတ်ရလွယ်ပါတယ်
  • Path တိုင်းကို path.join နဲ့ တည်ဆောက်ပြီး script folder အတွက် __dirname ကို သုံးပါ
  • http handler က method နဲ့ url ကို ကိုယ်တိုင် စစ်ပြီး branch တိုင်းမှာ res.end() ခေါ်ရပါတယ်
  • Practice/test တွေမှာ mkdtemp နဲ့ port 0 ကို သုံးပြီး ပြီးတာနဲ့ server.close() နဲ့ fs.rm() နဲ့ ရှင်းပါ

Node.js API index — Node.js

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

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

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

Practice: Core Modules (fs, path, http) | Thuta Learning