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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-router-controller-khmer

v1.5.2

Published

A lightweight, TypeScript-first decorator-based controller library for Express.js. Inspired by frameworks like NestJS and routing-controllers, this library makes it easy to organize your Express routes using classes and decorators.

Readme

express-router-controller-khmer

A lightweight, TypeScript-first decorator-based controller library for Express.js. Inspired by frameworks like NestJS and routing-controllers, this library makes it easy to organize your Express routes using classes and decorators.


Features

  • Class-based controllers with route decorators (@Get, @Post, etc.)
  • Parameter decorators for easy access to request data (@Body, @Param, @Query, @Req, @Res)
  • Prefix and middleware support at the controller and method level
  • TypeScript support out of the box
  • Pluggable error and response interceptors
  • Automatic class transformation and property decorators — Seamlessly transform and validate request data into class instances using decorators like @Transform (with callbacks such as ToNumber) for type conversion and custom logic.

Installation

npm install express-router-controller-khmer

Quick Start

1. Setup Express

import express from "express";
import { AutoRegisterControllers } from "express-router-controller-khmer";
import path from "path";

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const router = express.Router();

await AutoRegisterControllers({
  router,
  controllerPath: [path.join(__dirname, "controllers/*.js")],
  logging: true,
  classTransform: true, // Enable class transformation feature
});

app.use(router);
app.listen(5000, () => {
  console.log("Listening on port 5000");
});

2. Create a Controller and Use Class Transform

You can define DTO (Data Transfer Object) classes with property decorators to automatically transform and validate incoming request data. This is especially useful for type conversion and input sanitization.

Example DTO with Property Decorators

import { Transform, ToNumber } from "express-router-controller-khmer";

export default class UserDto {
  @Transform(ToNumber)
  age: number;

  @Transform(value => value.trim())
  name: string;
}

Example Controller Using DTO

import { Prefix, Post, Body } from "express-router-controller-khmer";
import UserDto from "./dto/user.dto";

@Prefix("/users")
export default class UserController {
  @Post("")
  createUser(@Body() data: UserDto) {
    // data.age is a number, data.name is trimmed
    return data;
  }
}

How it works:

  • When classTransform: true is enabled, the library will automatically instantiate your DTO classes and apply all property transformations defined by @Transform and built-in decorators.
  • This ensures that incoming data is properly typed and formatted before it reaches your business logic.
  • You can create your own custom property decorators using the @Transform utility.

Decorators

  • @Prefix(path: string) — Set a base path for the controller.
  • @Get(path: string) — Handle GET requests.
  • @Post(path: string) — Handle POST requests.
  • @Delete(path: string) — Handle DELETE requests.
  • @Put(path: string) — Handle PUT requests.
  • @Param(name?: string) — Inject route parameter(s).
  • @Query(name?: string) — Inject query parameter(s).
  • @Body() — Inject request body.
  • @Req() — Inject the raw Express request object.
  • @Res() — Inject the raw Express response object.
  • @Middleware(fn) — Attach middleware to a route.
  • @Transform(fn) — Transform a property value when using class transformation.
  • ToNumber — A callback function for @Transform that converts a value to a number (usage: @Transform(ToNumber)).

Middleware

You can add middleware at the method level using the @Middleware decorator:

import { Middleware } from "express-router-controller-khmer";
import authMiddleware from "./authMiddleware";

@Middleware(authMiddleware)
@Delete(":/id")
deleteUser(@Param("id") id: string, @Res() res) {
  // ...
}

Error and Response Interceptors

You can provide your own error and response interceptors for custom handling:

// Response Interceptor Example
import {IResponseInterceptor} from "express-router-controller-khmer";
import {Request, Response} from "express";

export default class ResponseInterceptor implements IResponseInterceptor {
    response(data: any, req: Request, res: Response) {
        res.json({data});
    }
}
// Error Interceptor Example
import {IErrorInterceptor} from "express-router-controller-khmer";
import {Request, Response} from "express";

class ErrorInterceptor implements IErrorInterceptor {
    errorException(error: any, req: Request, res: Response) {
        res.status(error.code || 500).json({message: error.message || "internal error"});
    }
}

Best Practices

  • Always use express.json() and express.urlencoded() before registering controllers.
  • Do not mutate shared state in your controllers or handlers.
  • Use TypeScript for best experience and type safety.
  • Use class transformation and property decorators to ensure your input data is clean and correctly typed. Use transformation callbacks like ToNumber with @Transform for type conversion.

License

MIT


Contributing

Pull requests and issues are welcome! Please open an issue to discuss your ideas or report bugs.


Author

Xiao Din 🔥