본문으로 건너뛰기

RouterModule

Express 기반 HTTP 서버를 설정하는 메인 모듈입니다. 미들웨어, 라우팅, Swagger UI, 헬스체크를 자동 구성합니다.

Import

import { RouterModule } from '@asapjs/router';

시그니처

class RouterModule {
public app: express.Application;
constructor(dirname: string);
}

파라미터

이름타입설명
dirnamestring라우트 파일이 위치한 디렉토리 경로. 이 경로의 route.ts(또는 route.js)를 로드합니다.

속성

이름타입설명
appexpress.ApplicationExpress 애플리케이션 인스턴스

자동 구성 내역

미들웨어 (자동 적용)

  • cors() — CORS 허용
  • bodyParser.json({ limit: '200mb' }) — JSON 파싱
  • bodyParser.urlencoded({ limit: '200mb' }) — URL-encoded 파싱
  • trust proxy 설정
  • config의 middleware 배열에 있는 커스텀 미들웨어

엔드포인트 (자동 등록)

경로설명
/{basePath}서버 상태 확인
/health-checkDB 헬스체크 (@asapjs/sequelize 연동)
/{basePath}/docs/swagger-ui.htmlSwagger UI
/{basePath}/docs/swagger.jsonSwagger JSON
/{basePath}/sync모델 동기화 (@asapjs/sequelize 확장)

Swagger 인증

config에서 Swagger UI 인증을 설정할 수 있습니다:

{
"swagger": {
"useAuth": true,
"userObject": {
"admin": "password"
}
}
}

사용 예제

import { Application } from '@asapjs/core';
import { RouterModule } from '@asapjs/router';
import { initSequelizeModule } from '@asapjs/sequelize';

const app = new Application(__dirname);
await initSequelizeModule(__dirname + '/models');

const router = new RouterModule(__dirname);
router.app.listen(3000, () => {
console.log('Server running on port 3000');
});

route.ts 파일 구조

// src/route.ts
import UserController from './controllers/UserController';

export default [
{ basePath: '/users', router: new UserController().router },
];

관련 API