Express모듈 활용
- 미들웨어 함수 : 요청 오브젝트, 응답 오브젝트, 요청-응답 주기 중 그 다음의 미들웨어 함수(next)에 대한 액세스 권한을 갖는 함수
- 미들웨어 함수가 요청-응답 주기를 res.end(), res.json()등으로 종료하지 않는 경우, next() 함수를 호출해, 다음 미들웨어 함수에 제어를 전달해야 한다.
const express = require('express');
const app = express()
const port = 3000
app.use((req, res, next) => {
console.log(new Date());
next();
});
app.get('/multiHandler', (req, res, next) => {
console.log('Handler1');
next();
}, (req, res) => {
res.send("Handler2");
})
app.get('/page/:user', (req, res, next) => {
if(req.params.user === "admin"){
next('route');
} else {
next();
}
}, (req, res) => {
res.send("general user");
})
app.get('/page/:user', (req, res) => {
res.send("admin user");
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
출처
https://expressjs.com/ko/guide/routing.html