Thuta Learning
ရှာဖွေရန်
IntermediateWeb Developmentbeginner

Serving Static Files

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

Static files ဆိုတာ server က generate မလုပ်ဘဲ file အတိုင်းပြန်ပေးတဲ့ HTML, CSS, browser JavaScript, image, font စတာတွေပါ။ Express မှာ express.static() middleware နဲ့ static folder တစ်ခုကို public အဖြစ် serve လုပ်နိုင်ပါတယ်။

javascript
const express = require('express');
const path = require('path');

const app = express();

app.use(express.static(path.join(__dirname, 'public')));

app.get('/api/message', (req, res) => {
  res.json({ message: 'API and static files can work together.' });
});

app.listen(3000, () => {
  console.log('Static server running at http://localhost:3000');
});

app.use(express.static(...)) က public folder ထဲက files တွေကို browser ကတိုက်ရိုက်ရယူနိုင်အောင်လုပ်ပါတယ်။ ဥပမာ public/index.html ရှိရင် http://localhost:3000 မှာပြနိုင်ပါတယ်။

You should see
Static server running at http://localhost:3000 Browser shows public/index.html if it exists.

Info

Static folder ထဲမှာ sensitive file, secret key, database backup မထည့်ပါနဲ့။ Public folder ဆိုတဲ့အတိုင်း browser ကရနိုင်တဲ့နေရာဖြစ်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • public folder path မှားရင် file မတွေ့ဘဲ 404 ဖြစ်ပါမယ်။ path.join(__dirname, 'public') ပုံစံသုံးတာက path issue လျော့စေပါတယ်။

လေ့ကျင့်ခန်း

Frontend build output, landing page assets, uploaded public images, documentation site files စတာတွေကို static folder နဲ့ serve လုပ်နိုင်ပါတယ်။

You'll know it worked when:

Serving Static Files | Thuta Learning