Koa로 프로젝트 세팅

jiho·2019년 12월 3일
0

Koa는 Express 개발팀에서 많은 부분을 제거한 좀더 가벼운 노드JS 백엔드 프레임워크입니다.

많이 빨리 공부하려고 OOP 언어로 서버를 구축하기보다는 가볍게 많은 컨셉을 이해하는 방향으로 Koa와 ReactJS, mongoDB를 이용해서 구현해나가보려고 합니다.

늘 NodeJS는 환경 세팅이 시간을 많이 걸리는 것같아서 기억할 의미로 순서를 작성해두겠습니다.

프로젝트 구성

프로젝트 생성과 koa 모듈 설치

yarn init -y
yarn add koa

ESlint 와 Prettier

VSCode를 사용해서 일관성 있는 코드 스타일 유지를 위해
Prettier-Code formatter와 ESLint 확장프로그램을 설치합니다.
해당 확장 프로그램들이 이용할 lib들도 yarn을 통해 설치해주도록 하겠습니다.

yarn add -D eslint
yarn run eslint --init

이어서 나오는 질문들은 만들고자하는 프로젝트에 맞게 정해줍니다.
저는 CommonJS, ES6로 설정했습니다. (추가적으로 계속 바뀔 예정)

.eslintrc.json

{
  "env": {
    "browser": true,
    "node": true,
    "es6": true
  },
  "extends": ["eslint:recommended", "prettier"],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
    "ecmaVersion": 2018
  },
  "rules": {
    "no-unused-vars": "warn",
    "no-console": "off"
  },
  "parser": "babel-eslint" // Babel을 이용해서 transpiling하기 위해서 설정
}

.prettierrc

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

Babel 과 Nodemon 설정

최신 JS코드를 작성하기 위해 Babel을 설치해줍니다.
yarn add -D @babel/core @babel/node @babel/preset-env babel-eslint
파일 수정시 서버를 재시작 할 수 있게 해주는 nodemon
yarn add -D nodemon

마지막으로 package.json 을 기록해두고 다음 포스트로 이어가겠습니다.

package.json

{
  "name": "blog-backend",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "node src",
    "start:dev": "nodemon --watch src/ --exec babel-node src/index.js"
  },
  "dependencies": {
    "dotenv": "^8.2.0",
    "eslint-config-prettier": "^6.7.0",
    "koa": "^2.11.0",
    "koa-bodyparser": "^4.2.1",
    "mongoose": "^5.7.13"
  },
  "devDependencies": {
    "@babel/core": "^7.7.4",
    "@babel/node": "^7.7.4",
    "@babel/preset-env": "^7.7.4",
    "babel-eslint": "^10.0.3",
    "eslint": "^6.7.2",
    "nodemon": "^2.0.1"
  }
}
profile
Scratch, Under the hood, Initial version analysis

0개의 댓글