본문 바로가기
Research/Server

Koa_간단한 async/await 구현하기

by RIEM 2023. 10. 31.
// index.js
const Koa = require('koa');

const app = new Koa();

app.use(async (ctx, next) => {
  console.log(ctx.url);
  console.log(1);
  if (ctx.query.authorized !== '1') {
    ctx.status = 401; // Unauthorized
    return;
  }
  // next().then(() => {
  //   console.log('Done :D');
  // });
  await next();
  console.log('Done :DD');
});

app.use((ctx, next) => {
  console.log(2);
  next();
});

app.use((ctx) => {
  ctx.body = 'hello world';
});

app.listen(4000, () => {
  console.log('Listening to port 400');
});

'Research > Server' 카테고리의 다른 글

Koa_라우팅  (0) 2023.11.01
Koa_파라미터 조건에 따른 미들웨어 실행하기  (0) 2023.10.31
서버_AWS EC2 Ubuntu에 NGinx 설치하는 방법  (0) 2023.03.06
서버_Nginx란  (0) 2023.03.06
PM2 모듈 기본 사용법  (1) 2023.03.01

댓글