@flexent/http-router

中文文档
2.22.1发布于 2026/11/13MITES Module零依赖近 30 天下载 2308

Decorator-based routing for HTTP server

原始使用文档
文档目录3 节
  1. Highlights
  2. Usage
  3. Routing rules

@flexent/http-router

Decorator-based routing for HTTP server.

Highlights

  • 🎯 Declarative route and parameter definitions
  • 🧩 Chain-friendly composition
  • ✅ Schema-based parameter validation

Usage

Define route handlers by decorating async functions with route and parameter decorators.

export class UsersRouter {

    @dep() private userStorage!: UserStorage;

    @Get({ path: '/users' })
    async getUsers(
        @QueryParam('limit', { schema: { type: 'number', default: 10 } }) limit: number,
        @QueryParam('offset', { schema: { type: 'number', default: 0 } }) offset: number,
    ) {
        const users = await this.userStorage.listUsers(limit, offset);
        return {
            users,
        };
    }

    @Post({ path: '/users' })
    async createUser(
        @BodyParam('name', { schema: { type: 'string' } }) name: string,
        @BodyParam('email', { schema: { type: 'string', regex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ } }) email: string,
        @BodyParam('password', { schema: { type: 'string', minLength: 8 } }) password: string,
    ) {
        const user = await this.userStorage.createUser(name, email, password);
        return { user };
    }

}

Bind routes to mesh. Most apps would use a separate scope for isolating request-scoped state.

Also bind RouterHandler: it will resolve all routes defined in the same scope.

export class HttpScope extends Mesh {

    constructor(parent: Mesh) {
        super('Http', parent);
        this.service(HttpRouteHandler);
        this.service(UsersRouter);
    }

}

In HttpServer, instantiate the scope and delegate request handling to RouterHandler.

export class MainHttpServer extends HttpServer {

    @dep() private mesh!: Mesh;

    async handle(ctx: HttpContext, next: HttpNext) {
        const scope = new HttpScope(this.mesh);
        await scope.resolve(HttpRouteHandler).handle(ctx, next);
    }

}

HttpRouteHandler can be part of chain, in that case it is handy to create a dedicated handler:

export class AppHandler extends HttpChain {

    @dep() private authHandler!: AuthHandler;
    @dep() private errorHandler!: ErrorHandler;
    @dep() private routeHandler!: HttpRouteHandler;

    handlers = [
        this.errorHandler,
        this.authHandler,
        this.routeHandler,
    ];
}

Alternatively, each router can inherit from HttpRouter.

export FooRouter extends HttpRouter {

    @Get({ path: '/foo' })
    async getFoo() {
        return {
            foo: 123,
        };
    }

}

This way routers can be composed into chains as per usual, to have more granular control over the order in which route handlers are called.

export class AppHandler extends HttpChain {

    @dep() private authHandler!: AuthHandler;
    @dep() private errorHandler!: ErrorHandler;
    @dep() private fooRouter!: FooRouter;

    handlers = [
        this.errorHandler,
        this.authHandler,
        this.fooRouter,
    ];
}

Note: HttpRouteHandler is generally more efficient because it can skip instantiating routers that do not match the request (see below).

Routing rules

  • There are two types of routes:

    1. Endpoints are defined with @Get, @Post, @Put, @Delete, @Patch decorators.
    2. Middlewares are defined with @Middleware decorator.
  • On each request a single endpoint is matched.

  • If an endpoint matches:

    • Middlewares from the same class are executed, in the order they are defined.
    • next() is not called, so any other remaining handlers in the chain are skipped.
  • If no endpoints match:

    • No middlewares are executed, even if they match.
    • next() is called to allow other handlers to execute.
    • As an additional optimization, if RouteHandler is used, it will only instantiate a router class with matching endpoint.

版本与发布记录

@flexent/http-router 共发布 40 个版本,首次发布于 2026/04/19;过去 12 个月发布了 40 个版本。

版本发布日期距今
2.22.1latest2026/11/132 天前
2.22.02026/11/132 天前
2.21.02026/11/132 天前
2.20.22026/11/133 天前
2.20.12026/11/115 天前
2.20.02026/11/069 天前

关于 @flexent/http-router 的常见问题

@flexent/http-router 是什么?

Decorator-based routing for HTTP server

如何安装 @flexent/http-router?

在项目目录运行 npm install @flexent/http-router,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:airtight(^5.7.2)、mesh-ioc(^4.3.1)、mesh-config(^1.3.4)、reflect-utils(^1.1.3)、@flexent/errors(^2.22.1)、@flexent/logger(^2.22.1)、mesh-decorators(^1.3.6)、@flexent/metrics(^2.22.1)、reflect-metadata(^0.2.2)、@flexent/http-server(^2.22.1)、@flexent/pathmatcher(^2.22.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@flexent/http-router 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flexent__http-router 是否存在。

@flexent/http-router 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@flexent/http-router 可以商用吗?

@flexent/http-router 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flexent/http-router 的下载量有多少?

根据 npm 官方下载统计,@flexent/http-router 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,308 次,日均约 77 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flexent/http-router 还在维护吗?

最新版本 2.22.1 发布于 2026/11/13(2 天前);至今共发布 40 个版本,其中过去 12 个月发布了 40 个;首次发布于 2026/04/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。