aotoyae

[React] json-server : 간단한 DB & API 서버 생성 패키지 본문

React

[React] json-server : 간단한 DB & API 서버 생성 패키지

aotoyae 2024. 2. 16. 20:22

 

 

💡 json-server 를 이용해 간단한 서버를 만들어보자

 

 

먼저 npm install json-server or yarn add json-server ~

 

설치가 끝나면 루트 경로에db.json 파일을 하나 만들고 데이터를 넣어준다.

{
  "posts": [
    { "id": "1", "title": "a title", "views": 100 },
    { "id": "2", "title": "another title", "views": 200 }
  ],
  "comments": [
    { "id": "1", "text": "a comment about post 1", "postId": "1" },
    { "id": "2", "text": "another comment about post 1", "postId": "1" }
  ],
  "profile": {
    "name": "typicode"
  }
}

 

그리고 터미널에 아래처럼 입력 ! port 는 react 와 겹치지 않게 4000 으로 지정

json-server --watch db.json --port 4000

 

 

그럼 저 귀여운 애가 db.json 를 바라보고 있다고 한다..

 

 

주소에 접근하면 넣어뒀던 데이터를 볼 수 있다!

 

 

 

🔗 https://www.npmjs.com/package/json-server

 

json-server

> [!IMPORTANT] > Viewing alpha v1 documentation – usable but expect breaking changes. For stable version, see [here](https://github.com/typicode/json-server/tree/v0). Latest version: 1.0.0-alpha.23, last published: 21 days ago. Start using json-server in

www.npmjs.com