JSON Server

April·2022년 2월 8일
0

🌱server

목록 보기
6/18
post-thumbnail

Getting started

json-serverjson 파일을 사용하여 간단한 시뮬레이션을 위한 REST API Mock server를 구축할 수 있는 툴이다


✔️ JSON Server 설치

npm install -g json-server

yarn add json-server

✔️ db.json 생성해서 초기 데이터 추가하기

  • 프로젝트 루트 폴더에 fake-server 폴더 생성 후
  • db.json 생성해서 초기 데이터 추가하기
{
  "todos": [
    {
      "id": 1,
      "title": "메모 제목",
      "body": "메모 내용"
    },
    {
      "id": 2,
      "title": "todo1",
      "body": "todo1"
    },
    {
      "id": 3,
      "title": "todo2",
      "body": "todo2"
    }
  ]
}

✔️ JSON Server 시작하기

// 기본 포트는 3000
json-server --watch db.json

// 포트 변경해서 실행하기
json-server --watch db.json --port 3001

✔️ JSON ServerCRUD 적용하기

db.json에 생성한 이름이 경로가 된다.
예시) {
"todos": [
ㅤㅤ{
ㅤㅤㅤ"id": 1,
ㅤㅤㅤㅤ"title": "메모 제목",
ㅤㅤㅤ"body": "메모 내용"
ㅤㅤ},
ㅤ]
}

methodpath
GET/todos
GET/todos/1
POST/todos
PUT/todos/1
PATCH/todos/1
DELETE/todos

profile
🚀 내가 보려고 쓰는 기술블로그

0개의 댓글