21.1 소개하기

21.2 작업 환경 준비

// Node 설치 확인
node --version

// koa 설치
yarn add koa

//eslint 설치
yarn add --dev eslint
yarn run eslint --init

//prettier 설정
{
  "singleQuote": true,
  "semi": true,
  "useTabs": false,
  "tabWidth": 2,
  "trailingComma": "all",
  "printWidth": 80
}

21.3 Koa 기본 사용법

21.3.1 서버 띄우기

const Koa = require('koa');

const app = new Koa();

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

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

21.3.2 미들웨어

  • next 함수는 Promise를 반환
  • Koa는 async/await을 지원

21.4 nodemon 사용하기

//nodemon 설치
yarn add --dev nodemon

//package.json
//yarn start:dev로 시작하면 nodemon은 src 디렉터리를 주시하고 있다고 해당 폴더 안의 변화를 감지하여 src/index.js를 재시작
"scripts" : {
	"start" : "node src",
    "start:dev" : "nodemon --watch src/ src/index.js"
}

21.5 koa-router 사용하기

+ Recent posts