TIL 231129 - JSON SERVER

두두맨·2023년 11월 29일
1

JSON SERVER란 ?

아주 간단한 DB와 API 서버를 생성해주는 패키지.
실제 API 서버 구축되기 전까지 임시 사용할 mock 데이터 만들기 위함.

  • yarn add json-server 로 설치
  • yarn start
  • yarn json-server --watch db.json --port 3001

두 가지 각자 따로 실행. 각자 실행해줘야 리액트와 제이슨 서버가 통신 가능함.

  • db.json에 객체 형식 데이터 입력해줌.
{
  "todos": [
    {
      "id": 1,
      "title": "json-server",
      "content": "json-server를 배워봅시다."
    }
  ]
}
  • h ttp://localhost:3001/todos 로 이동하면 넣어준 todos 확인 가능함.
  • 배포

    • server 코드 작성. // server 폴더 최상위 경로에 생성하고 그 안에 index.js 생성
  • cross-env 설치

    • yarn add cross-env

scripts": {
    "start": "node server",
    "start:dev": "cross-env NODE_PATH=src react-scripts start",
    "build": "cross-env NODE_PATH=src react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "heroku-postbuild": "cross-env NODE_PATH=src npm run build"
  },

// 원래코드

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},
  
  • yarn build

  • node server

profile
병아리 개발준비생 🐥

0개의 댓글