模块路径:
src/middleware/+src/handler/+src/routes/生成时间: 2026-05-11
请求处理模块是 LLM-Router 的核心业务逻辑层,负责 HTTP 请求的认证、CORS 处理、模型别名、角色重写、参数过滤和路由分发。通过 Express 中间件链实现横切关注点的分离。
Authorization: Bearer <key> 头401 Unauthorized204 No Contentaliases 字段502 Bad Gatewayrole_rewrites 配置重写消息角色unsupported_params 配置移除请求参数"stream": true 参数{ name, version, description } JSONauthMiddleware(config: Config): RequestHandlercorsMiddleware(): RequestHandlerpreprocessRequest(config: Config): RequestHandlerchatCompletionsHandler(proxies: ProxyInstances): RequestHandlerapp.use(corsMiddleware); // CORS 预检
app.use(authMiddleware); // Bearer Token 认证
app.use(express.json()); // JSON 解析
app.post('/chat/completions', chatCompletionsHandler);
app.get('/health', healthCheckHandler);
app.all('*', defaultProxyHandler);
| Go 版本 | TypeScript 版本 | 说明 |
|---|---|---|
handler.HandleRequest() 单函数 |
Express 中间件链 | 横切关注点分离 |
| 内嵌认证逻辑 | authMiddleware() |
独立中间件 |
| 内嵌 CORS | corsMiddleware() |
独立中间件 |
| 内嵌预处理 | preprocessRequest() |
独立中间件 |
http.ServeMux |
Express Router | 路由注册 |