Skip to content

配置

使用 yasui.createServer()yasui.createApp() 的 YasuiJS 应用程序完整配置参考。

概述

YasuiJS 提供两种主要方式来创建应用程序:

  • yasui.createServer(config) - 自动创建并启动 HTTP 服务器
  • yasui.createApp(config) - 返回一个可手动配置的 Express 应用程序

这两种方法接受相同的配置对象,具有以下选项。

配置选项

必需选项

controllers

类型: Array<Constructor>
描述: 要在应用程序中注册的控制器类数组。

typescript
import yasui from 'yasui';

yasui.createServer({
  controllers: [UserController, ProductController]
});

可选选项

middlewares

应用于所有请求的全局中间件数组。可以是 YasuiJS 中间件类或 Express RequestHandler 函数。

  • 类型: Array<Constructor | RequestHandler>
  • 默认值: []
  • 示例值: [LoggingMiddleware, cors()]

globalPipes

应用于所有路由参数的全局管道数组。详见 Pipes

  • 类型: Array<Constructor<IPipeTransform>>
  • 默认值: []
  • 示例值: [ValidationPipe, TrimPipe]

environment

应用程序的环境名称。

  • 类型: string
  • 默认值: process.env.NODE_ENV || 'development'
  • 示例值: production

port

HTTP 服务器的端口号。仅用于 createServer()

  • 类型: number | string
  • 默认值: 3000

protocol

用于服务器 URL 日志记录的协议。目前仅用于显示目的。

  • 类型: 'http' | 'https'
  • 默认值: 'http'

debug

启用调试模式,提供额外的日志记录和请求跟踪。

  • 类型: boolean
  • 默认值: false

injections

依赖注入的自定义注入令牌。详见 依赖注入

  • 类型: Array<{ token: string, provide: any }>
  • 默认值: []
  • 示例值:
typescript
[
  { token: 'DATABASE_URL', provide: 'postgresql://localhost:5432/mydb' },
  { token: 'CONFIG', provide: { apiKey: 'secret' } }
]

swagger

Swagger 文档配置。详见 Swagger

  • 类型: SwaggerConfig | undefined
  • 默认值: undefined
  • 示例值:
typescript
{
  enabled: true,
  path: '/api-docs',
  info: {
    title: 'My API',
    version: '1.0.0',
    description: 'API documentation'
  }
}

enableDecoratorValidation

启用启动时的装饰器验证以捕获配置错误。

  • 类型: boolean
  • 默认值: true

createServer() 与 createApp() 的比较

createServer()

创建 HTTP 服务器并自动开始监听:

typescript
import yasui from 'yasui';

yasui.createServer({
  controllers: [UserController],
  port: 3000,
  debug: true
});

使用场景:

  • 想要立即启动服务器
  • 不需要额外的 Express 配置
  • 构建简单的 API

createApp()

返回一个可手动配置的 Express 应用程序:

typescript
import yasui from 'yasui';

const app = yasui.createApp({
  controllers: [UserController]
});

// 添加自定义 Express 中间件
app.use('/health', (req, res) => {
  res.json({ status: 'ok' });
});

// 添加自定义路由
app.get('/custom', (req, res) => {
  res.json({ message: 'Custom route' });
});

// 手动启动服务器
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

使用场景:

  • 需要自定义 Express 配置
  • 想要添加自定义路由或中间件
  • 需要更多服务器启动控制
  • 与现有 Express 应用程序集成

配置示例

基本 API 设置

typescript
yasui.createServer({
  controllers: [UserController, AuthController],
  port: 3000,
  debug: true
});

完整配置

typescript
yasui.createServer({
  controllers: [UserController, AuthController],
  middlewares: [LoggingMiddleware, AuthMiddleware],
  globalPipes: [ValidationPipe, TrimPipe],
  port: 3000,
  protocol: 'http',
  debug: false,
  environment: 'production',
  enableDecoratorValidation: true,
  injections: [
    { token: 'DATABASE_URL', provide: process.env.DATABASE_URL },
    { token: 'JWT_SECRET', provide: process.env.JWT_SECRET }
  ],
  swagger: {
    enabled: true,
    path: '/api-docs',
    info: {
      title: 'My API',
      version: '1.0.0',
      description: 'Complete API with all features'
    }
  }
});

Express 集成

typescript
import yasui from 'yasui';
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';

const app = yasui.createApp({
  controllers: [UserController],
  middlewares: [LoggingMiddleware]
});

// 添加 Express 中间件
app.use(cors());
app.use(helmet());
app.use(express.json({ limit: '10mb' }));

// 添加自定义路由
app.get('/health', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({ error: 'Something went wrong!' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

调试模式

启用调试模式以查看详细信息:

typescript
yasui.createServer({
  controllers: [UserController],
  debug: true
});

调试模式提供:

  • 请求/响应日志
  • 依赖注入详情
  • 路由注册信息
  • 错误堆栈跟踪

根据AGPL v3许可证发布。