카테고리 없음

[Next.js + express] controller 와 middleware 그리고 handler

개린이다 2023. 3. 6. 15:39

* 궁금증

- 강의를 수강하던 도중, controller 과 middleware의 관계에 대해 잘 이해가 가지 않아 이에 대해 찾아보고 정리해보려고 한다. 이와 함께 각각의 정의에 대해서도 간단하게 다루어보려고 한다.

 

1. controller

1) controller 란?

웹 어플리케이션에서 컨트롤러란, 외부의 요청을 처리하는 모듈을 의미한다. 정확하게 말하자면 하나 이상의 클라이언트가 보내는 요청을 처리하고 요청을 보낸 클라이언트에게 응답을 반환하는 역할이라 보면 된다.

그렇기에 컨트롤러의 목적은 애플리케이션에 대한 특정요청을 수신하는 것이며, 라우팅 매커니즘은 어떤 컨트롤러가 해당 요청을 처리할지 조정한다. 

 

*라우팅이란?

라우팅 네트워크에서는 어떠한 패킷을 원하는 곳으로 보내는 행위를 가리키며, 여기서 라우팅이란 들어온 HTTP 요청을 특정 컨트롤러로 보내는 행위를 말한다. 

 

2. middle ware

1) middle ware 란?

- 미들웨어란 클라이언트로부터 들어온 요청을 각 컨트롤러의 요청 핸들러가 처리하기 이전에 코드를 실행할 수 있는 기능이다. 미들웨어 함수는 애플리케이션의 요청-응답 주기에서 요청 및 응답에 접근할 수 있으며, next() 라는 미들웨어 함수를 사용할 수 있다. 

express 에서는 middleware에 next()를 호출하지 않을시, 요청이 보류되므로 꼭 써주기!

 

 

그러니까, 즉.

컨트롤러 중에 미들웨어가 있는거!

컨트롤러는 핸들러를 묶어주는? 그룹화 해주는 느낌? 이라고 볼 수 있을 거 같음

왜냐면 컨트롤러가 외부의 요청을 처리하는 모듈이니까!

 

*핸들러란

- 이벤트 핸들러 : 어떠한 사건에 대한 동작을 다룬다

- 예시

: 마우스 이벤트 들(mouseover, mouseover )

(지금까지 우리가 했던 건, 이벤트 핸들러 이기에 이벤트 핸들러를 정의했음)

 

 

https://www.wisewiredbooks.com/nestjs/overview/02-controller-1.html

 

컨트롤러 1편 - 쉽게 풀어 쓴 Nest.js

웹 어플리케이션에서 컨트롤러(Controller)란 외부의 요청을 처리하는 모듈을 의미합니다. 좀 더 정확하게 말하면 하나 이상의 클라이언트가 보내는 요청을 처리하고 요청을 보낸 클라이언트에게

www.wisewiredbooks.com

https://expressjs.com/en/guide/routing.html

 

Express routing

Routing Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.ge

expressjs.com

 

https://expressjs.com/en/guide/using-middleware.html

 

Using Express middleware

Using middleware Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls. Middleware functions are functions that have access to the request ob

expressjs.com

 

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes

 

Express Tutorial Part 4: Routes and controllers - Learn web development | MDN

We've now created all the routes for our site, along with dummy controller functions that we can populate with a full implementation in later articles. Along the way we've learned a lot of fundamental information about Express routes, and some approaches f

developer.mozilla.org

 

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes

 

Express Tutorial Part 4: Routes and controllers - Learn web development | MDN

We've now created all the routes for our site, along with dummy controller functions that we can populate with a full implementation in later articles. Along the way we've learned a lot of fundamental information about Express routes, and some approaches f

developer.mozilla.org

 

https://velog.io/@korea_webclass/javascript-%EC%9D%B4%EB%B2%A4%ED%8A%B8-%ED%95%B8%EB%93%A4%EB%9F%ACEvent-Handler%EB%9E%80

 

javascript : 이벤트 핸들러 (Event Handler)란?

이벤트 핸들러는 "어떠한 사건에 대한 동작을 다룬다" 라는 의미입니다. 웹에서 콘텐츠를 열면 내용이 나오고 닫기버튼을 누르면 닫히는 것들이 다 이벤트 핸들러입니다. 이벤트 핸들러 개념은

velog.io