birdpack
v1.1.0
Published
BirdPack web framework is a tool for web server via TCP HTTP supporting websocket focusing on speed.
Readme
🐦 birdpack
birdpack คือ Web Framework สำหรับ Node.js ที่ถูกออกแบบมาให้ เบา, เร็ว, ใช้ง่าย, รองรับ SSL หลายโดเมน พร้อมระบบ Routing และ Static File Server
📦 การติดตั้ง
npm install birdpack🚀 เริ่มต้นใช้งาน (Quick Start)
const fs = require('fs');
const birdpack = require('birdpack');
const app = new birdpack({
ssl: [
{
key: fs.readFileSync(__dirname + "/backend/ssl/test.dev.key"),
cert: fs.readFileSync(__dirname + "/backend/ssl/test.dev.crt"),
domain: ['test.dev'],
},
],
traffic: '$time [$method] $path $ip',
log: 'console',
logFile: `${__dirname}/server.log`,
port: 443,
use: 'https',
});
// Routing พื้นฐาน
app.get('/', (req) => req.send('hi'))
.post('/json', (req) => req.code(400).json({ err: 400 }))
.all('/allpath', (req) => req.send('test'))
.directory('/public'); // Static files
// Routing เฉพาะ domain
let test = app.domain('test.dev');
test.next((req, next) => {
if (req.look('token')) next({ rank: 'admin' });
else next({ rank: 'user' });
})
.get('/user/:id', (req, data) => {
req.json({ userId: req.params.id, role: data.rank });
})
.get('/home', (req) => req.file('index.html'))
.routes(__dirname + '/backend/routes');
// เริ่ม server
app.listen();📂 โครงสร้างโปรเจกต์แนะนำ
my-app/
├── backend/
│ ├── routes/
│ │ └── index.js
│ └── ssl/
│ ├── test.dev.crt
│ └── test.dev.key
├── public/
├── server.log
└── index.js⚙️ การตั้งค่า (Config Options)
| Key | Type | Default | Description |
| --- | --- | --- | --- |
| ssl | Array | [] | SSL Certificates { key, cert, domain[] } |
| traffic | string | false | รูปแบบ log เช่น $time $method $path $ip |
| log | string | 'console' | 'console' | 'file' | false |
| logFile | string | - | path log file (ใช้เมื่อ log:'file') |
| port | number | 80 | Port ที่ server listen |
| use | string | 'http' | 'http' หรือ 'https' |
🔀 Routing Methods
app.get('/hello', req => req.send('Hello World'));
app.post('/data', req => req.json({ ok: 1 }));
app.all('/all', req => req.send('Match any method'));
app.directory('/public');
app.routes(__dirname + '/backend/routes');| Method | Description | | --- | --- | | .get(path, handler) | รับเฉพาะ GET | | .post(path, handler) | รับเฉพาะ POST | | .all(path, handler) | รองรับทุก Method | | .directory(folder) | เสิร์ฟ static files | | .routes(path) | โหลด routes จากไฟล์/โฟลเดอร์ | | .domain(domain) | แยก routing ตามโดเมน |
🧩 Request Object (req)
📤 ส่ง Response
req.send("text");
req.json({ hello: "world" });
req.html("<h1>Hi</h1>");
req.code(201).send("Created");
req.file("index.html");
req.redirect("https://google.com");📌 Headers
req.set("Content-Type", "text/plain");
let ua = req.get("user-agent");🔍 Query Parameters (?id=1&user=2)
req.u("id"); // "1"
req.look("id,user"); // ตรวจว่ามี id และ user
req.query; // {id:"1", user:"2"}📥 Payload
req.q("username");
req.check("id,user");
req.body; // payload object🍪 Cookies
req.scookie("token", "abc123", { httpOnly: true });
req.rcookie("token");
console.log(req.cookie);⚙️ Response Config
req.length(1024);
req.type("application/json");📌 Properties
req.ip; // IP client
req.host; // Host
req.method; // GET/POST
req.path; // Path
req.params; // เช่น /user/:id => {id:"123"}🧭 Middleware + Domain Routing
let dashboard = app.domain('dashboard.dev');
dashboard.next((req, next) => {
if (!req.look('token'))
return req.code(401).send('Unauthorized');
next({ user: 'admin' });
});
dashboard.get('/stats', (req, data) => {
req.json({ status: 'ok', user: data.user });
});📂 การใช้งาน Routes แบบอัตโนมัติ
คุณสามารถใช้ app.routes(folderPath) เพื่อให้ birdpack โหลดเส้นทาง (routes) จากไฟล์ในโฟลเดอร์ที่กำหนด โดย ชื่อไฟล์ จะถูกแปลงเป็น Method และ Path อัตโนมัติ
📌 กฎการตั้งชื่อไฟล์
| รูปแบบชื่อไฟล์ | HTTP Method | Path ที่ได้ |
| --- | --- | --- |
| get@user-id-$id.js | GET | /user/id/:id |
| post@user-*.js | POST | /user/* |
| [email protected] | ALL | /health |
💡 รูปแบบ: {method}@{path}.js
ใช้ $param เพื่อกำหนด dynamic params เช่น $id
ใช้ * เพื่อ match ทุก segment ที่เหลือ
📂 โครงสร้างตัวอย่าง
backend/
└── routes/
├── get@user-id-$id.js
├── post@user-*.js
└── [email protected]📌 ตัวอย่างไฟล์ route
// backend/routes/get@user-id-$id.js
module.exports = (req) => {
req.json({ userId: req.params.id });
};
// backend/routes/post@user-*.js
module.exports = (req) => {
req.html('<h1>Post request matched!</h1>');
};
// backend/routes/[email protected]
module.exports = (req) => {
req.send('OK');
};📌 การใช้งานใน main server
const app = new birdpack({...});
app.routes(__dirname + '/backend/routes');
app.listen();📝 Logging Variables
| Placeholder | Description | | --- | --- | | $time | เวลา | | $method | HTTP Method | | $path | Path ของ request | | $ip | IP ของ client | | $status | HTTP Status | | $body | Payload | | $head(key) | Header value เช่น $head(user-agent) |
traffic: '$time - $ip - $method $path [$status] UA:$head(user-agent)'📌 License
R938 Service
