1. 싱글톤 패턴
한 클래스의 객체(인스턴스)가 하나밖에 생성하지 않는 방식을 싱글톤 패턴
이라고 한다.
장점은 메모리가 덜 낭비되는 것이라고 하는데 아직 직접 겪어보지는 못했다.
2. 기존 코드
import express, { NextFunction, Request, Response } from "express";
import helmet from "helmet";
import morgan from "morgan";
import authRouter from "./auth/auth.router";
import config from "./config";
import db from "./database";
import rateLimiter from "./middleware/rateLimiter";
import tweetRouter from "./tweet/tweet.router";
const app = express();
app.use(express.json());
app.use(morgan("dev"));
app.use(helmet());
app.use(rateLimiter);
app.use("/auth", authRouter);
app.use("/tweet", tweetRouter);
app.use((req: Request, res: Response, next: NextFunction) => {
res.status(404).send("NOT FOUND");
});
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
res.status(500).send(err);
});
db.sync().then(() => {
app.listen(config.port, () => {
console.log("Server On...");
});
});
3. 싱글톤 패턴 적용
import express, { NextFunction, Request, Response } from "express";
import helmet from "helmet";
import morgan from "morgan";
import authRouter from "./auth/auth.router";
import config from "./config";
import db from "./database";
import rateLimiter from "./middleware/rateLimiter";
import tweetRouter from "./tweet/tweet.router";
import cors from "cors";
class Server {
private app: express.Application;
constructor() {
this.app = express();
}
// 라우터
private setRouter() {
this.app.use("/auth", authRouter);
this.app.use("/tweet", tweetRouter);
}
// 에러 핸들러
private setErrorHandler() {
this.app.use((req: Request, res: Response, next: NextFunction) => {
res.status(404).send("NOT FOUND");
});
this.app.use(
(err: any, req: Request, res: Response, next: NextFunction) => {
res.status(500).send(err);
}
);
}
// 미들웨어
private setMiddleware() {
this.app.use(express.json());
if (config.nodeEnv === "production") {
console.log(`config.nodeEnv === "production"`);
this.app.use(morgan("combined"));
} else {
console.log(`config.nodeEnv === "develop"`);
this.app.use(morgan("dev"));
}
this.app.use(helmet());
this.app.use(rateLimiter);
this.app.use(cors());
this.setRouter();
this.setErrorHandler();
}
// DB 연결 & 서버 실행
public listen() {
this.setMiddleware();
db.sync().then(() => {
this.app.listen(config.port, () => {
console.log("Server On...");
});
});
}
}
function init() {
const server = new Server();
server.listen();
}
init();