Middleware
: request-response 주기 안에서 request 객체, response 객체, 그리고 next 함수에 접근할 수 있는 권한을 가진 함수를 의미합니다. 그리고 next 함수는 지금의 미들웨어 함수가 성공하면 실행될 힘수를 뜻합니다.
Middleware 함수는 아래의 목록의 일을 할수 있습니다.
- Excute any code 모든 코드를 실행
- Make changes to the request and the response object 요청 및 응답 오브젝트에 대한 변경을 실행
- End the request-response cycle. 요청-응답 주기를 종료.
- Call the next middleware in the stack 스택 내의 다음 미들웨어를 호출.
var express = require('express');
var app = express();
var requestTime = function (req, res, next) { // 이름이 'requestTime'인 미들웨어 함수
req.requestTime = Date.now(); //req에 requestTime이라는 필드를 추가했습니다.
next();
};
app.use(requestTime); // use(
app.get('/', function (req, res) {
var responseText = 'Hello World!';
responseText += 'Requested at: ' + req.requestTime + '';
res.send(responseText);
});
app.listen(3000);
Express는 자체적인 최소한의 기능을 갖춘 라우팅 및 미들웨어 웹 프레임워크입니다.
Express 애플리케이션은 기본적으로 일련의 미들웨어 함수 호출입니다.
기본적으로 애플리케이션, 라우터, 오류처리, 기본제공, 써드파티 레벨로 미들웨어들이 나뉩니다.
작업의 종류의 따라 나눈 거라고 생각하면 간단합니다.