HANA -J

WIL [항해99] 8주차 - 내가 공부한 것 본문

what I Learnd/WIL

WIL [항해99] 8주차 - 내가 공부한 것

Hana-J 2021. 12. 26. 21:04
  • express 에서 싱글톤 패턴사용 =>  app.js파일에서 전역에서 사용되는 인스턴스를 딱 1개만 생성시켜 메모리 낭비를 줄일 수 있다.
class Server {
    public app :express.Application;

    constructor(){
        const app:express.Application= express();
        this.app = app;
    }
    private setRoute(){
        this.app.use(catsRouter); 
    }
    private setMiddleware(){
        //* json middleware 
        this.app.use(express.json());

        this.setRoute();

        //404 미들웨어 맨 아래
        this.app.use((req, res, next)=>{
            res.send({error :"404 not found error"})
        })
    }
    public listne(){
        this.setMiddleware();
        this.app.listen(port, () => {
            console.log(`Example app listening at http://localhost:${port}`)
          });
    }
}

function init(){
    const server = new Server();
    server.listne();
}

init();
  • 서비스 패턴 => router 와 DB에 연결되는 service 로직분리 => 가독성이 좋고, 후에 유지 보수에 좋다.
// * route.js
router.get('/users', readAllusers);


// * service.js
export const readAllusers = (req, res)=>{
	try{
    // DB에서 찾아오는 로직
    
    }catch(error){
    	res.status(400).send({error})
    }
}
728x90

'what I Learnd > WIL' 카테고리의 다른 글

WIL [항해99] 11주차  (0) 2022.01.16
WIL [항해99] 7주차 - 내가 공부한 것  (0) 2021.12.19
WIL [항해99] 6주차 - 협업이란  (0) 2021.12.11
WIL [항해99] 5주차 - 같이 일하고 싶은 개발자  (0) 2021.12.05
WIL -ORM , SQL  (0) 2021.11.28
Comments