https://opentutorials.org/course/3370
생활코딩님의 Express.js강의를 참고하였습니다.
1.next()
미들웨어와 라우트 핸들러 사이에서 제어를 넘기기 위해 사용
app.use(function (req, res, next) {
console.log('Time:', Date.now());
next();
});
2.여러개의 미들웨어
여러개의 미들웨어가 하나의 라우트 핸들러가 있으면
next()를 통해 해당 라우트핸들러 내부의 다음 미들웨어로 넘어간다.
next('route')를 한다면 다음 라우트핸들러로 제어가 넘어간다.
app.get('/user/:id', function (req, res, next) {
// if the user ID is 0, skip to the next route
if (req.params.id == 0) next('route');
// otherwise pass the control to the next middleware function in this stack
else next(); //
}, function (req, res, next) {
// render a regular page
res.render('regular');
});
// handler for the /user/:id path, which renders a special page
app.get('/user/:id', function (req, res, next) {
res.render('special');
});
반응형
'BackEnd > Express.js' 카테고리의 다른 글
[Express.js] 정적파일 (0) | 2024.09.10 |
---|---|
[Express.js] 미들웨어 제작하기 (0) | 2024.09.10 |
[Express.js] compression (0) | 2024.09.09 |