728x90
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');
});
파라미터로 가져올 때
쿼리로 가져올 때
728x90
'Research > Server' 카테고리의 다른 글
Koa_간단한 async/await 구현하기 (0) | 2023.10.31 |
---|---|
Koa_파라미터 조건에 따른 미들웨어 실행하기 (0) | 2023.10.31 |
서버_AWS EC2 Ubuntu에 NGinx 설치하는 방법 (0) | 2023.03.06 |
서버_Nginx란 (0) | 2023.03.06 |
PM2 모듈 기본 사용법 (1) | 2023.03.01 |
댓글