npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

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 dengan admin
  • */admin/* -> Semua path yang ada admin 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')
})