728x90
Koa에서 next()를 생략하면 다음 미들웨어는 실행하지 않는다. 이 원리를 이용해서 파라미터의 값에 따라 화면을 다르게 렌더링할 수 있다.
// index.js
const Koa = require('koa');
const app = new Koa();
app.use((ctx, next) => {
console.log(ctx.url);
console.log(1);
if (ctx.query.authorized !== '1') {
ctx.status = 401; // Unauthorized
return;
}
next();
});
app.use((ctx, next) => {
console.log(2);
next();
});
app.use((ctx) => {
ctx.body = 'hello world';
});
app.listen(4000, () => {
console.log('Listening to port 400');
});
localhost:4000/ 접속 시
localhost:4000/?authorized=1 접속 시
728x90
'Research > Server' 카테고리의 다른 글
Koa_라우팅 (0) | 2023.11.01 |
---|---|
Koa_간단한 async/await 구현하기 (0) | 2023.10.31 |
서버_AWS EC2 Ubuntu에 NGinx 설치하는 방법 (0) | 2023.03.06 |
서버_Nginx란 (0) | 2023.03.06 |
PM2 모듈 기본 사용법 (1) | 2023.03.01 |
댓글