Express.js က Node.js web server ရေးတာကိုပိုလွယ်စေတဲ့ framework ပါ။ Raw http module နဲ့ route တွေခွဲရင် code များလာပေမယ့် Express မှာ app.get(), app.post(), middleware တွေနဲ့ အလွယ်တကူစီမံနိုင်ပါတယ်။
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello from Express!');
});
app.get('/health', (req, res) => {
res.json({ status: 'ok', service: 'nodejs-tutorial' });
});
app.listen(port, () => {
console.log(`Express app running at http://localhost:${port}`);
});express() က Express app တစ်ခုဖန်တီးပါတယ်။ app.get('/') က home route ဖြစ်ပြီး browser က home path ကိုလာရင် text ပြန်ပေးပါတယ်။ /health route က JSON response ပြန်ပေးတဲ့ small API endpoint ဖြစ်ပါတယ်။
Express app running at http://localhost:3000 GET /health result: { "status": "ok", "service": "nodejs-tutorial" }Info
Express မှာ route order အရေးကြီးပါတယ်။ Specific route တွေကို အပေါ်မှာထားပြီး fallback/404 route တွေကိုအောက်ဆုံးမှာထားပါ။