ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Website တစ်ခုမှာ အကြောင်းအရာ နှစ်မျိုး ရှိပါတယ်။ တစ်မျိုးက request တိုင်း server က အသစ် တွက်ချက်ပေးရတဲ့ အရာ — ဥပမာ user ရဲ့ order list ပါ။ နောက်တစ်မျိုးကတော့ ဘယ်သူ တောင်းတောင်း အတူတူပဲ ဖြစ်တဲ့ file တွေ — logo ပုံ၊ CSS file၊ browser မှာ run မယ့် JavaScript တွေပါ။ ဒုတိယအမျိုးကို "static file" လို့ ခေါ်ပါတယ်။
Static file တစ်ခုချင်းအတွက် route ရေးစရာ မလိုပါဘူး။ express.static('public') လို့ ပြောလိုက်ရင် Express က ပြခန်းမှာ ပစ္စည်းတွေ ခင်းထားသလို public folder ကို ဖွင့်ပေးပြီး browser က /css/site.css လို့ တောင်းတိုင်း public/css/site.css ကို ရှာပြီး ပြန်ပေးပါတယ်။ ပြခန်းထဲ ထည့်ထားသမျှ လူတိုင်း မြင်ရတာမို့ ဘာထည့်မလဲ ဆိုတာ သတိထားရပါမယ်။
Problem → Why → Solution
ပြဿနာ: file တစ်ခုချင်းအတွက် route တစ်ခုစီ
Static middleware မသိခင် developer တစ်ယောက်က file တစ်ခုချင်းကို readFile နဲ့ ဖတ်ပြီး route တစ်ခုစီ ရေးတယ် ဆိုပါစို့။ ပုံအသစ် တစ်ပုံ ထည့်တိုင်း route အသစ် လိုပြီး Content-Type ကိုလည်း ကိုယ်တိုင် ခန့်မှန်းရပါတယ်၊ မှားရင် browser က CSS ကို CSS အဖြစ် မသုံးပါဘူး။ ပိုဆိုးတာက req.params ကနေ file နာမည်ကို ယူပြီး ဖတ်လိုက်တော့ ../server.js လို request က source code ကိုပါ ပေါက်ကြားစေနိုင်ပါတယ်။
app.get('/style.css', async (req, res) => {
res.type('text/plain'); // wrong type: CSS ignored
res.send(await readFile('public/style.css', 'utf8'));
});
app.get('/logo.png', /* ...another route... */); // one route per file
app.get('/files/:name', async (req, res) => {
res.send(await readFile('public/' + req.params.name)); // traversal risk
});ဘာကြောင့် express.static() က ပိုကောင်းတာလဲ
express.static() က URL ကို folder ထဲက file နဲ့ အလိုအလျောက် ချိတ်ပေးပြီး file extension ကိုကြည့်ကာ Content-Type မှန်မှန် ထည့်ပေးပါတယ်။ Root folder အပြင်ကို .. နဲ့ ထွက်ဖို့ ကြိုးစားတဲ့ path တွေကို ပယ်ပြီး ETag၊ Last-Modified၊ Cache-Control လို caching header တွေကိုလည်း ထည့်ပေးပါတယ်။ ကိုယ်ရေးတဲ့ code က folder တစ်ခုကို ညွှန်တဲ့ line တစ်ကြောင်းတည်း ဖြစ်သွားပါတယ်။
Core concept: URL path = folder ထဲက file path
app.use(express.static(dir)) ဆိုရင် request ရဲ့ path ကို dir နဲ့ ဆက်ပြီး file ကို ရှာပါတယ်။ /css/site.css က dir/css/site.css ဖြစ်ပြီး / လို / နဲ့ဆုံးတဲ့ directory path အတွက် default အနေနဲ့ index.html ကို ရှာပါတယ်။ URL ထဲမှာ folder နာမည် public မပါပါဘူး — /public/css/site.css လို့ ခေါ်ရင် dir/public/css/site.css ကို ရှာမှာမို့ မတွေ့ပါဘူး။ File ရှိရင် middleware က response ကို ပို့ပြီး ကွင်းဆက် ရပ်ပါတယ်၊ မရှိရင် next() ခေါ်ပြီး request ကို အောက်က route တွေဆီ လွှဲပေးပါတယ်။
express.static('public') လို relative path ပေးရင် Node က process ကို စ run ခဲ့တဲ့ folder (cwd) ကနေ ရှာပါတယ်။ Project folder အပြင်ကနေ run ရင် folder ကို ရှာမတွေ့ဘဲ file တိုင်း 404 ဖြစ်ပါတယ်။ ဒါကြောင့် path.join(__dirname, 'public') (ESM မှာ path.join(import.meta.dirname, 'public')) လို absolute path ကို အမြဲ ပေးပါ။
app.use('/static', express.static(dir)) လို path prefix နဲ့ mount လုပ်ရင် /static/app.js က dir/app.js ကို ပြန်ပေးပါတယ်။ API route တွေနဲ့ နာမည်တူ ပဋိပက္ခ မဖြစ်အောင်၊ caching rule ကို static file တွေအတွက်ပဲ သီးသန့် ထားနိုင်အောင် prefix သုံးတာ ကောင်းပါတယ်။ Static middleware ကို routes တွေရဲ့ အပေါ်မှာ ထားရင် disk ကို အရင် စစ်ပြီး အောက်မှာ ထားရင် route တွေကို အရင် စစ်ပါတယ် — file နဲ့ route နာမည်တူနေရင် ဘယ်သူ နိုင်လဲ ဆိုတာ ဒီ အစဉ်က ဆုံးဖြတ်ပါတယ်။
Security ဘက်ကလည်း static middleware က အလုပ်တချို့ လုပ်ပေးပါတယ်။ Path ကို root folder နဲ့ ဆက်ပြီး normalize လုပ်ကာ root အပြင်ကို .. နဲ့ ထွက်ဖို့ ကြိုးစားတဲ့ request ကို ပယ်ချပါတယ်။ . နဲ့စတဲ့ file နဲ့ folder တွေကိုလည်း default အနေနဲ့ မရှိသလို ဆက်ဆံပါတယ်။ ဒါပေမယ့် folder ထဲမှာ ကိုယ်တိုင် ထည့်ထားတဲ့ ကျန် file တွေကိုတော့ စစ်ဆေးမှု မရှိဘဲ ပေးပါတယ်။
| Option | Default | ဘာလုပ်လဲ |
|---|---|---|
| index | 'index.html' | Directory request အတွက် ပြမယ့် file၊ false ဆို ပိတ် |
| maxAge | 0 | Browser cache ထားနိုင်တဲ့ အချိန် ('1d' = 86400 စက္ကန့်) |
| dotfiles | 'ignore' | .env လို . နဲ့စတဲ့ file တွေကို မတွေ့သလို 404 ပြန် |
| fallthrough | true | File မတွေ့ရင် error မပေးဘဲ next() ခေါ် |
- Prefix ကို ဖယ်ပြီး path ကို ယူ
- root + path ကို disk ပေါ်မှာ ရှာ
- တွေ့ရင် type + cache header နဲ့ ပို့
- မတွေ့ရင် next() → route/404
Static folder ထဲက file တိုင်း public ဖြစ်တယ်လို့ ယူဆပါ — server code နဲ့ secret တွေကို အဲဒီ folder အပြင်မှာ ထားပါ။
အထွေထွေ ပုံစံ
const dir = path.join(__dirname, 'public'); // absolute, not cwd-relative
app.use(express.static(dir)); // /file.ext -> dir/file.ext
app.use('/prefix', express.static(dir, { // /prefix/file.ext -> dir/file.ext
maxAge: '1d', // Cache-Control max-age
index: false, // no index.html for folders
}));ပထမပုံစံက folder ကို site ရဲ့ root မှာ ဖွင့်ပြီး ဒုတိယပုံစံက prefix အောက်မှာ option တွေနဲ့ ဖွင့်ပါတယ်။ Folder တစ်ခုထက် ပိုလည်း mount လုပ်လို့ ရပြီး Express က မှတ်ပုံတင်တဲ့ အစဉ်အတိုင်း ရှာပါတယ်။
Example: static file တွေနဲ့ API တစ်ခု အတူ
Example က temporary folder ထဲမှာ project သေးသေးလေး ဆောက်ပါတယ် — public/ ထဲမှာ index.html၊ css/site.css နဲ့ မှားထည့်မိတဲ့ .env file ပါပြီး public ရဲ့ အပြင်မှာ secret.txt ရှိပါတယ်။ ပြီးရင် request ငါးခု ပို့ပြီး ဘာ serve ဖြစ်လဲ၊ ဘာ မဖြစ်ဘူးလဲ print ထုတ်ကာ server ပိတ်ပြီး temp folder ကို ဖျက်ပါတယ်။
import express from 'express';
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
// Build a tiny project in a temp folder: public/ is served, secret.txt is not
const root = await mkdtemp(path.join(tmpdir(), 'static-demo-'));
const publicDir = path.join(root, 'public');
await mkdir(path.join(publicDir, 'css'), { recursive: true });
await writeFile(path.join(publicDir, 'index.html'), '<h1>Shop</h1>');
await writeFile(path.join(publicDir, 'css', 'site.css'), 'h1 { color: teal; }');
await writeFile(path.join(publicDir, '.env'), 'API_KEY=oops');
await writeFile(path.join(root, 'secret.txt'), 'database password');
const app = express();
app.use(express.static(publicDir));
app.get('/api/message', (req, res) => {
res.json({ message: 'API and static files can work together.' });
});
const server = app.listen(0, async () => {
const base = `http://localhost:${server.address().port}`;
for (const url of ['/', '/css/site.css', '/api/message', '/.env', '/secret.txt']) {
const res = await fetch(base + url);
const type = res.headers.get('content-type');
const body = res.ok ? await res.text() : '(not served)';
console.log(`${url.padEnd(13)} ${res.status} ${type?.split(';')[0]} ${body}`);
}
server.close();
await rm(root, { recursive: true, force: true });
});Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Mounting the folder
app.use(express.static(publicDir));publicDir က mkdtemp ကနေ ရတဲ့ absolute path ပါ။ Real project မှာ path.join(__dirname, 'public') ကို အလားတူ သုံးပါတယ်။
- index.html for /
await writeFile(path.join(publicDir, 'index.html'), '<h1>Shop</h1>');/ ကို တောင်းတဲ့အခါ route မရှိပေမယ့် index option ရဲ့ default ကြောင့် public/index.html ကို ပြန်ပေးပါတယ်။
- Correct types
const type = res.headers.get('content-type');Output မှာ .html က text/html၊ .css က text/css ဖြစ်တာကို တွေ့ရပါတယ် — extension ကိုကြည့်ပြီး Express က သတ်မှတ်ပေးတာပါ။
- Falling through to the API
app.get('/api/message', (req, res) => {public/api/message ဆိုတဲ့ file မရှိလို့ static middleware က next() ခေါ်ပြီး request က ဒီ route ဆီ ရောက်ပါတယ်။
- What stays private
await writeFile(path.join(publicDir, '.env'), 'API_KEY=oops');.env က dotfiles: 'ignore' default ကြောင့် 404 ရပြီး secret.txt က root folder အပြင်မှာ ရှိလို့ URL နဲ့ လုံးဝ မရောက်နိုင်ပါဘူး။ ဒါပေမယ့် .env ကို public ထဲ ထည့်တာ ကိုယ်တိုင်က အမှားပါ — default ကို အားမကိုးပါနဲ့။
/ 200 text/html <h1>Shop</h1>
/css/site.css 200 text/css h1 { color: teal; }
/api/message 200 application/json {"message":"API and static files can work together."}
/.env 404 text/html (not served)
/secret.txt 404 text/html (not served)Browser cache နဲ့ 304 Not Modified
express.static() က file တိုင်းနဲ့ ETag (file ရဲ့ fingerprint) နဲ့ Last-Modified header ကို ပို့ပါတယ်။ Browser က အဲဒီ file ကို နောက်တစ်ခါ တောင်းတဲ့အခါ If-None-Match header နဲ့ ETag ကို ပြန်ပို့ပြီး file မပြောင်းရင် server က body မပါတဲ့ 304 Not Modified ကိုပဲ ပြန်ပေးပါတယ် — download ပြန်မလုပ်ရလို့ မြန်ပါတယ်။ maxAge က ဒီထက် တစ်ဆင့် ပိုသွားပါတယ်။ သတ်မှတ်ထားတဲ့ အချိန်အတွင်း browser က server ကို လုံးဝ မမေးဘဲ ကိုယ့် cache ထဲကပဲ သုံးပါတယ်။
First visit, revisit, and revisit within maxAge
-----------------------------------------------
visit 1: GET /css/site.css -> 200 + body + ETag "abc"
visit 2: GET /css/site.css
If-None-Match: "abc" -> 304 (no body, file unchanged)
maxAge: browser reuses its copy -> no request sent at allလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Express နဲ့ server-rendered site တွေမှာ public/ folder ထဲမှာ CSS၊ ပုံ၊ font နဲ့ browser JavaScript တွေ ထားပြီး template တွေက <link href="/css/site.css"> လို ညွှန်ကြပါတယ်။ React ဒါမှမဟုတ် Vue app ကို build လုပ်ရင် ထွက်လာတဲ့ dist/ folder ကို express.static() နဲ့ serve ပြီး API ကို /api အောက်မှာ ထားတာလည်း အဖြစ်များတဲ့ ပုံစံပါ။ Build tool တွေက file နာမည်ထဲ app.3f9a1c.js လို hash ထည့်ပေးလို့ အဲဒီ file တွေကို maxAge ရှည်ရှည်နဲ့ cache လုပ်ပြီး index.html ကိုတော့ cache မလုပ်ကြပါဘူး။
Traffic များတဲ့ production မှာတော့ static file တွေကို Node ကနေ မပို့ဘဲ Nginx ဒါမှမဟုတ် CDN (Cloudflare၊ CloudFront) ကနေ ပို့လေ့ ရှိပါတယ်၊ ဒါမှ Node process က API request တွေအတွက် အားလပ်နေပါတယ်။ ဒါပေမယ့် app သေးသေး၊ internal tool နဲ့ development မှာတော့ express.static() တစ်ခုတည်းနဲ့ လုံလောက်ပါတယ်။ ရှေ့မှာ CDN ရှိနေရင်တောင် public folder ထဲ ဘာထည့်မလဲ ဆိုတဲ့ စည်းမျဉ်း အတူတူပါပဲ၊ ဘာလို့လဲဆိုတော့ CDN က ကိုယ့် server ပေးချင်တာမှန်သမျှကို ကူးယူသွားလို့ပါ။ Search engine မှာ ပေါ်မလာစေချင်တဲ့ file မျိုးဆိုရင် အဲဒီ folder ထဲမှာ နေရာ မရှိပါဘူး။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. URL ထဲမှာ folder နာမည် ထည့်ရေးတာ
express.static(publicDir) ကို root မှာ mount လုပ်ထားပြီး HTML ထဲမှာ <link href="/public/css/site.css"> လို့ ရေးရင် 404 ရပါတယ်။ Folder နာမည်က URL ထဲ မပါပါဘူး — /css/site.css လို့ ရေးပါ၊ ဒါမှမဟုတ် /public prefix နဲ့ mount လုပ်ပါ။
2. Project folder တစ်ခုလုံးကို static လုပ်တာ
express.static(__dirname) လို့ ရေးလိုက်ရင် server.js၊ package.json နဲ့ config file တွေ အားလုံး URL နဲ့ download လုပ်လို့ ရသွားပါတယ်။ Static file တွေအတွက် public/ လို သီးသန့် folder တစ်ခု ထားပြီး အဲဒါကိုပဲ serve ပါ။
3. Relative path နဲ့ folder ကို ညွှန်တာ
express.static('public') က project folder ထဲကနေ run မှ အလုပ်လုပ်ပြီး PM2၊ systemd ဒါမှမဟုတ် တခြား folder ကနေ run ရင် file တိုင်း 404 ဖြစ်ပါတယ်။ path.join(__dirname, 'public') ကို သုံးပါ။
4. Hash မပါတဲ့ file ကို maxAge ရှည်ရှည် ပေးတာ
site.css ကို maxAge: '30d' နဲ့ serve ပြီးနောက် CSS ပြင်လိုက်ရင် user တွေရဲ့ browser က file အဟောင်းကို ရက် ၃၀ ဆက်သုံးနေပါတယ်။ ရှည်ကြာတဲ့ cache ကို နာမည်မှာ hash ပါတဲ့ file တွေမှာပဲ သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- File နာမည်မှာ hash ပါတဲ့ build output တွေကို maxAge ရှည်ရှည်နဲ့၊ index.html ကို cache မပါဘဲ serve ပါ။
- Upload လုပ်တဲ့ user file တွေကို code နဲ့ ရောမထားဘဲ သီးသန့် folder နဲ့ prefix အောက်မှာ ထားပါ။
- Static middleware ကို API route တွေ အပေါ်မှာ ထားရင် /api/... နဲ့ ပဋိပက္ခ မဖြစ်အောင် prefix ခွဲထားပါ။
Mini challenge
Mini challenge: cache လုပ်ထားတဲ့ /static assets
Temp folder ထဲမှာ asset file နှစ်ခု ဆောက်ပြီး /static prefix အောက်မှာ cache တစ်ရက်နဲ့ serve ကာ index listing မရှိတဲ့ Express app တစ်ခု ရေးပါ။ Request လေးခုနဲ့ status၊ type နဲ့ Cache-Control ကို စစ်ပါ။
- assets/app.js နဲ့ assets/img/logo.svg ကို mkdtemp folder ထဲမှာ ဖန်တီးရမယ်
- app.use('/static', express.static(folder, { maxAge: '1d', index: false })) သုံးရမယ်
- ကျန်တဲ့ request တွေ JSON 404 ရရမယ်
- Server ပိတ်ပြီးနောက် temp folder ကို ဖျက်ရမယ်
import express from 'express';
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
// TODO: create a temp folder with app.js and img/logo.svg
const app = express();
// TODO: mount the folder at /static with maxAge '1d' and index false
// TODO: JSON 404 handler
// TODO: listen on port 0; fetch '/static/app.js', '/static/img/logo.svg',
// '/app.js', '/static/'; print status, type and cache-control;
// close the server and remove the temp folderရလဒ်: ရရမယ့် output: /static/app.js 200 text/javascript public, max-age=86400 /static/img/logo.svg 200 image/svg+xml public, max-age=86400 /app.js 404 application/json no cache header /static/ 404 application/json no cache header
အရိပ်အမြွက် ကြည့်ရန်
maxAge: '1d' က Cache-Control: public, max-age=86400 ကို ထုတ်ပေးပါတယ်။ index: false ဆိုရင် /static/ ကို file မတွေ့သလို ယူဆပြီး next() ခေါ်လို့ 404 handler ဆီ ရောက်ပါတယ်။
အဖြေ ကြည့်ရန်
import express from 'express';
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
const assets = await mkdtemp(path.join(tmpdir(), 'assets-'));
await mkdir(path.join(assets, 'img'));
await writeFile(path.join(assets, 'app.js'), "console.log('hi');");
const svg = '<svg xmlns="http://www.w3.org/2000/svg"/>';
await writeFile(path.join(assets, 'img', 'logo.svg'), svg);
const app = express();
app.use('/static', express.static(assets, { maxAge: '1d', index: false }));
app.use((req, res) => res.status(404).json({ error: 'Not found' }));
const server = app.listen(0, async () => {
const base = `http://localhost:${server.address().port}`;
const urls = ['/static/app.js', '/static/img/logo.svg', '/app.js', '/static/'];
for (const url of urls) {
const res = await fetch(base + url);
const type = res.headers.get('content-type')?.split(';')[0];
const cache = res.headers.get('cache-control') ?? 'no cache header';
console.log(`${url.padEnd(20)} ${res.status} ${type} ${cache}`);
}
server.close();
await rm(assets, { recursive: true, force: true });
});Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- express.static(dir) က URL path ကို dir ထဲက file path နဲ့ ချိတ်ပြီး Content-Type နဲ့ caching header ကို ထည့်ပေးတယ်။
- Folder ကို path.join(__dirname, 'public') လို absolute path နဲ့ ညွှန်ပြီး လိုရင် prefix နဲ့ mount လုပ်ပါ။
- File မတွေ့ရင် next() ခေါ်လို့ static file တွေနဲ့ API route တွေ app တစ်ခုတည်းမှာ အတူ နေနိုင်တယ်။
- Static folder ထဲက အရာအားလုံး public ပါ — secret၊ backup နဲ့ server code တွေကို အပြင်မှာ ထားပါ။
Serving static files — Express.js