1.2.1 โ€ข Published 5 months ago

@refkinscallv/express-routing-ts v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

๐Ÿ“ฆ @refkinscallv/express-routing-ts

Laravel-style routing system for Express.js in TypeScript. Simplify your route definitions, middleware stacks, and controller bindings like a boss.


๐Ÿ›  Installation

npm install @refkinscallv/express-routing-ts

๐Ÿงช See Example

Curious how it all comes together?
๐Ÿ‘‰ Check out example/index.ts for a full working demo!


๐Ÿ“š Features

  • โœ… Simple route declarations (get, post, etc.)
  • โœ… Grouped routes with prefix
  • โœ… Middleware stack: global, group, and per-route
  • โœ… Controller-method pair as route handler
  • โœ… Consistent handler format using HttpContext
  • โœ… Auto-instantiate controllers
  • โœ… Express-compatible

๐Ÿงฉ Type Definitions

type RouteMiddleware = (req: Request, res: Response, next: NextFunction) => void;

type RouteMethod = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options' | 'head';

type HttpContext = {
  req: Request;
  res: Response;
  next: NextFunction;
};

type RouteHandler = ((ctx: HttpContext) => any) | [any, string];

interface RouteDefinition {
  methods: RouteMethod[];
  path: string;
  handler: RouteHandler;
  middlewares?: RouteMiddleware[];
}

โœจ Usage

๐Ÿ”น 1. Basic Route

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

๐Ÿ”น 2. With Middleware

Routes.post(
  '/submit',
  ({ res }) => res.send('Submitted'),
  [authMiddleware],
);

๐Ÿ”น 3. Controller Binding

You can use [ControllerClass, 'methodName']:

class UserController {
  index({ res }: HttpContext) {
    res.send('User list');
  }
}

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

โš ๏ธ Controller will be instantiated if not passed as an object.


๐Ÿ”น 4. Grouped Routes

Use Routes.group() to prefix and stack middleware:

Routes.group('/admin', () => {
  Routes.get('/dashboard', ({ res }) => res.send('Admin Dashboard'));
}, [adminOnly]);

๐Ÿ”น 5. Global Middleware Scope

Wrap multiple routes in a shared global middleware:

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

๐Ÿ”น 6. Register to Express

import express from 'express';
import Routes from '@refkinscallv/express-routing-ts';

// registered your routes
// import 'path/to/routes.ts'

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

Routes.apply(router);

app.use(router);
app.listen(3000);

๐Ÿ“– API Reference

๐Ÿ“Œ Routes Methods

MethodDescription
get()Define GET route
post()Define POST route
put()Define PUT route
patch()Define PATCH route
delete()Define DELETE route
options()Define OPTIONS route
head()Define HEAD route
add()Custom route with multiple methods

๐Ÿ“Œ Static Methods

MethodDescription
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 middlewares
Routes.middleware()Apply global middleware scope to nested routes
Routes.apply()Apply all registered routes to an Express router

๐Ÿ“Œ Execution Flow

Middleware Execution Order:

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

Handler Execution:

  • If a function โ†’ Executed directly
  • If [Controller, 'method']:

    • Instantiates controller (if class passed)
    • Binds and executes method

๐Ÿงช Example

class HomeController {
  index({ res }: HttpContext) {
    res.send('Welcome to Home!');
  }
}

Routes.group('/v1', () => {
  Routes.get('/', [HomeController, 'index']);
});

๐Ÿง  Tips

  • Route paths will be automatically cleaned to avoid duplicate slashes (// โ†’ /).
  • Controller methods are bound to their instance or static context.
  • Handler functions can be async or return a Promise.

๐Ÿค License

MIT License ยฉ 2025 [Refkinscallv]

1.2.1

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.2

5 months ago

1.0.0

5 months ago