본문 바로가기
Research/Server

Koa_라우팅

by RIEM 2023. 11. 1.
const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

router.get('/', (ctx) => {
  ctx.body = 'Home';
});

router.get('/about/:name?', (ctx) => {
  const { name } = ctx.params;
  ctx.body = name ? `${name}의 페이지` : 'About';
});

router.get('/posts', (ctx) => {
  const { id } = ctx.query;
  ctx.body = id ? `Post #${id}` : `There is no post..`;
});

// Koa app 인스턴스에 라우터 적용하기
app.use(router.routes()).use(router.allowedMethods());

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

 

 

파라미터로 가져올 때

 

쿼리로 가져올 때

 

 

댓글