Registry / devops / koatty_router

koatty_router

JSON →
library2.2.0jsnpmunverified

Koatty路由组件,支持HTTP/1.1、HTTP/2、WebSocket、gRPC和GraphQL协议的统一路由管理。当前版本2.2.0,要求Node >=18.0.0。采用工厂模式创建和管理多协议路由器,提供装饰器驱动的路由定义、参数验证、DTO支持以及路由级中间件管理(RouterMiddlewareManager)。支持四种gRPC流类型及背压控制。与koatty_core深度集成,适用于构建微服务架构的多协议后端应用。

npm install koatty_router
INSTALL
IMPORT
SIG · KOATTY_ROUTER
K
koatty_router
devopsjavascriptv2.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

NewRouter
import { NewRouter } from 'koatty_router'
const NewRouter = require('koatty_router').NewRouter
ESM-only since v2; CJS not supported.
Controller
import { Controller } from 'koatty_router'
import { Controller } from 'koatty_core'
Controller装饰器定义在koatty_router中,不是koatty_core。
Get
import { Get } from 'koatty_router'
import { GetMapping } from 'koatty_router'
Get是别名,GetMapping是完整名称,两者均可使用。
RouterFactory
import { RouterFactory } from 'koatty_router'
import { RouterFactory } from 'koatty_router/factory'
RouterFactory从主包导出,无需子路径。

演示使用NewRouter创建HTTP路由器、@Controller/@Get装饰器定义路由,以及启动应用。

import { NewRouter, Controller, Get } from 'koatty_router'; import { Koatty } from 'koatty_core'; const app = new Koatty(); // 创建HTTP路由器 const httpRouter = NewRouter(app, { protocol: 'http', prefix: '/api' }); // 定义控制器(需先在应用启动前注册) @Controller('/user') class UserController { @Get('/profile') async getProfile(@Get('id') id: string) { return { id, name: 'user' }; } } // 注册控制器(实际使用中由框架自动扫描) httpRouter.addController(UserController); // 启动应用 app.listen(3000, () => { console.log('Server running on port 3000'); });
Debug
Known issues
breakingv2.0.0 移除了CommonJS支持,仅支持ESM。
fix
将项目改为ESM模块(在package.json中添加type: module)或使用动态import。
affects: >=2.0.0
breakingv2.0.0 将中间件接口从IMiddleware改为Middleware装饰器模式。
fix
使用@Middleware()装饰器定义中间件类,而不是实现IMiddleware接口。
affects: >=2.0.0
gotchaRouterFactory.create()需要先调用getInstance()获取工厂实例。
fix
使用RouterFactory.getInstance().create(...)而非直接调用RouterFactory.create().
affects: >=2.0.0
deprecatedv2.2.0 中GetMapping、PostMapping等别名标记为deprecated,推荐使用Get、Post等短名称。
fix
将GetMapping替换为Get,PostMapping替换为Post,以此类推。
affects: >=2.2.0
gotchagRPC路由器需要外部protobuf编译器(protoc)生成JS代码,不内置。
fix
单独安装protoc并生成proto文件的JS/TS绑定,然后通过protoFile选项指定路径。
affects: >=2.0.0
Errors
Common errors & fixes
Error: Cannot find module 'koatty_router'
没有安装koatty_router或node_modules缺失。
fix
运行npm install koatty_router(确保package.json中有依赖,然后npm install)。
TypeError: NewRouter is not a function
CommonJS require导入方式,但v2只支持ESM。
fix
改为import { NewRouter } from 'koatty_router'并在package.json中添加type: module。
Reflect.getMetadata is not a function
缺少reflect-metadata shim。
fix
运行npm install reflect-metadata,并在应用入口文件顶部import 'reflect-metadata'(确保顺序)。
Protocol 'grpc' is not supported. Supported protocols are: http, https, ws, wss, grpc, graphql
传递了不支持的协议值或拼写错误。
fix
检查protocol参数,确保是'http'、'https'、'ws'、'wss'、'grpc'或'graphql'。
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies
koatty_corerequired提供Application、KoattyContext等核心类型和类
reflect-metadatarequired装饰器依赖元数据反射
Agent activity
4 hits · last 30 days
node
4
Resources
koatty_router — npm install koatty_router · libregistry