예제
.controller.ts
, controller.js
(js는 빌드 시를 대비)
import * as path from 'path';
import * as glob from 'glob';
import { ServerRouter } from './server-router';
export function AutoRegisterRoutesAll() {
const rootDir = path.join(require.main.path, "api");
const pattern = '**/*.controller.{ts,js}';
const contFiles = glob.sync(pattern, { cwd: rootDir });
contFiles.forEach((file) => {
const filePath = path.join(rootDir, file);
const target = require(filePath);
if(target == null) return;
let controller: any = undefined;
const vals = Object.values(target);
if(vals.length > 0) {
controller = vals[0];
}
if(controller === undefined) return;
ServerRouter.registerController(controller);
});
}