kontas-xroutes
v1.0.12
Published
To install dependencies:
Downloads
869
Readme
📚 Dokumentasi Kontas XRoutes
🔷 Cara Pakai
1. Install Package
npm install kontas-xroutes
2. Setup Basic
import express from 'express'
import { XRoutes, XMiddleware } from 'kontas-xroutes'
const app = express()
await XRoutes.createServer(app, { dir: 'src' })
app.listen(3000)
3. Struktur Folder
project/
├── src/
│ └── routes/ <- WAJIB nama "routes"
│ ├── users/
│ │ └── controller.ts
│ └── admin/
│ └── controller.ts
4. Format Controller
A. Format Function
export async function GET(req: Request, res: Response) {
res.json({ message: 'Hello' })
}
B. Format Class
export default class UserController {
static async getUsers(req: Request, res: Response) {
res.json({ users: [] })
}
}
🔷 Fitur Middleware
1. Setup Basic Middleware
XMiddleware.setup({
global: [
async (req, res, next) => {
console.log('Request masuk!')
next()
}
]
})
2. Route Specific Middleware
XMiddleware.setup({
routes: {
'/admin/*': [
async (req, res, next) => {
console.log('Admin route!')
next()
}
]
}
})
3. Method Specific Middleware
XMiddleware.setup({
routes: {
'/api/users': {
GET: [middleware1],
POST: [middleware2],
PUT: [middleware3],
DELETE: [middleware4]
}
}
})
🔷 Pattern Routes
1. Basic Pattern
/admin
-> Match persis/api
-> Match persis
2. Wildcard
/admin/*
-> Semua path di bawah/admin
*/admin
-> Semua path yang diakhiri denganadmin
*/admin/*
-> Semua path yang adaadmin
di tengahnya
3. Dynamic Route
users/[id]
->/users/:id
posts/[slug]
->/posts/:slug
4. Contoh Match
'/admin/*' Matches:
- ✅
/admin/users
- ✅
/admin/settings
- ❌
/admin
- ❌
/api/admin
🔷 Tips & Tricks
1. Middleware
- Global jalan duluan
- Route-specific kedua
- Method-specific terakhir
- Bisa combine beberapa middleware
2. Best Practices
- Bikin middleware reusable
- Selalu validasi input
- Tangani error dengan baik
- Gunakan TypeScript untuk kestabilan
- Dokumentasi jelas untuk tim
3. Error Handling
- Selalu gunakan try-catch
- Return error dengan deskripsi yang jelas
- Logging error itu penting
- Tangani error async dengan hati-hati
4. Performance
- Gunakan cache kalau perlu
- Minimalisir jumlah middleware
- Optimalkan panggilan database
- Monitor response time
🔷 Error Handler Otomatis
1. Setup Basic
Buat file errorHandler.ts
di folder src/
:
import { Request, Response, NextFunction } from 'express'
export const errorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {
// Logic error handling kmu di sini
res.status(500).json({
error: err.message || 'Internal Server Error'
})
}
2. Cara Kerja
- XRoutes otomatis detect file
errorHandler.ts
- Gk perlu setup manual di index
- Error handler akan aktif utk semua routes
- Best practice utk handle semua error
3. Tips Error Handler
- Bedain error berdasarkan status code
- Log error ke file/database
- Jgn expose stack trace ke client
- Handle async error dgn baik
🔷 Contoh Full
import express from 'express'
import { XRoutes, XMiddleware } from 'kontas-xroutes'
const app = express()
// Basic middleware
app.use(express.json())
// Setup middleware
XMiddleware.setup({
global: [
async (req, res, next) => {
console.log('Request masuk!')
next()
}
],
routes: {
'/admin/*': [
async (req, res, next) => {
console.log('Admin route!')
next()
}
],
'/api/users': {
GET: [middleware1],
POST: [middleware2]
}
}
})
// Create server
await XRoutes.createServer(app, { dir: 'src' })
app.listen(3000, () => {
console.log('Server jalan di port 3000')
})