import * as express from "express"
import catsRouter from "./cats/cats.route";
const port: number = 8000;
const app: express.Express = express()
app.use((req: express.Request, res: express.Response, next: any) => {
console.log(req.rawHeaders[1]);
console.log('this is logging middleware');
next();
});
app.use(express.json());
app.use(catsRouter);
app.use((req:express.Request, res:express.Response) => {
console.log('this is error middleware');
res.send({error: '404 not found error'});
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
import * as express from "express"
import catsRouter from "./cats/cats.route";
const port: number = 8000;
class Server {
public app: express.Application;
constructor() {
const app: express.Application = express();
this.app = app;
}
private setRoute() {
this.app.use(catsRouter);
}
private setMiddleware() {
this.app.use((req: express.Request, res: express.Response, next: any) => {
console.log(req.rawHeaders[1]);
console.log('this is logging middleware');
next();
});
this.app.use(express.json());
this.setRoute();
this.app.use((req:express.Request, res:express.Response) => {
console.log('this is error middleware');
res.send({error: '404 not found error'});
})
}
public listen() {
this.setMiddleware();
this.app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
}
}
function init() {
const server = new Server();
server.listen();
}
init();
✅ 하나의 인스턴스만을 찍어내고 사용하는 것이 바로 싱글톤 패턴이다.
import * as express from "express";
그건 아니다.
잘못 적용하면 SOLID 원칙에 위배되고 안티 패턴이 될 수도 있다고 한다.