Skip to content

refkinscallv/express-routing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ @refkinscallv/express-routing

Laravel-style routing system for Express.js in JavaScript. Clean route definitions, middleware support, and controller bindings β€” just like your favorite PHP framework, but in JS.


πŸ›  Installation

npm install @refkinscallv/express-routing

πŸ§ͺ Example Usage

Check out example/index.js for a full working demo of how this works in a real-world Express app.


πŸ“š Features

  • βœ… Simple and clean route declarations (get, post, etc.)
  • βœ… Grouped routes with prefix
  • βœ… Middleware stack: per-route and group-level
  • βœ… Controller-method pair as route handler
  • βœ… Supports HttpContext style handlers: { req, res, next }
  • βœ… Auto-binds controller methods
  • βœ… Fully Express-compatible

✨ Usage

πŸ”Ή 1. Basic Route

Routes.get('/hello', ({ res }) => {
  res.send('Hello World')
})

πŸ”Ή 2. With Middleware

const authMiddleware = (req, res, next) => {
  // auth logic
  next()
}

Routes.post('/secure', ({ res }) => res.send('Protected'), [authMiddleware])

πŸ”Ή 3. Controller Binding

class UserController {
  static index({ res }) {
    res.send('User List')
  }
}

Routes.get('/users', [UserController, 'index'])

⚠️ Class-based handlers will auto-bind to static or instance methods.


πŸ”Ή 4. Grouped Routes

Routes.group('/admin', () => {
  Routes.get('/dashboard', ({ res }) => res.send('Admin Panel'))
})

With middleware:

Routes.group('/secure', () => {
  Routes.get('/data', ({ res }) => res.send('Secure Data'))
}, [authMiddleware])

πŸ”Ή 5. Global Middleware Scope

Routes.middleware([authMiddleware], () => {
  Routes.get('/profile', ({ res }) => res.send('My Profile'))
})

πŸ”Ή 6. Apply to Express

const express = require('express')
const Routes = require('@refkinscallv/express-routing')

const app = express()
const router = express.Router()

// Register your routes
require('./routes')

Routes.apply(router)
app.use(router)

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

πŸ“– API Reference

πŸ“Œ Routes Methods

Method Description
get() Register a GET route
post() Register a POST route
put() Register a PUT route
patch() Register a PATCH route
delete() Register a DELETE route
options() Register an OPTIONS route
head() Register a HEAD route
add() Register multiple methods at once

πŸ“Œ Static Methods

Method Description
Routes.get() Register a GET route
Routes.post() Register a POST route
Routes.put() Register a PUT route
Routes.delete() Register a DELETE route
Routes.patch() Register a PATCH route
Routes.options() Register an OPTIONS route
Routes.head() Register a HEAD route
Routes.add() Register one or more HTTP methods at once
Routes.group() Group routes under a prefix and share middleware
Routes.middleware() Apply global middleware scope to nested routes
Routes.apply() Apply all defined routes to an Express router

πŸ“Œ Execution Flow

Middleware execution order:

[ Global Middleware ] β†’ [ Group Middleware ] β†’ [ Route Middleware ]

Handler execution:

  • If function β†’ executed directly
  • If [Controller, 'method'] β†’ auto-instantiated (if needed), method is called

🧠 Tips

  • All route paths are cleaned automatically to avoid double slashes (// β†’ /)
  • Controller methods are auto-bound (no bind() needed)
  • Handlers support async/Promise usage
  • Middleware order matters, just like native Express

πŸ§ͺ Run Example

npm run example

Visit: http://localhost:3000


πŸ“ License

MIT License Β© 2025 Refkinscallv


πŸ‘‹ Stay in Touch

Made with ❀️ by Refkinscallv Follow us for more tools that simplify Node.js development.