간단한 Json server / API 만들기

김명성·2022년 2월 16일
0

자바스크립트

목록 보기
11/26

터미널
mkdir restapijson (파일명 작성)
cd restapijson (진입)

npm install -g json-server (전역으로 json-server설치)

json-server --watch db.json (db는 파일명)

package.json 예시

{
  "name": "restapijson",
  "version": "1.0.0",
  "description": "no desc",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error : no test specified\" && exit 1",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/yourgithub/yourrepo.git"
  },
  "keywords": [
    "jsonapiserver"
  ],
  "author": "eremes kim",
  "license": "ISC",
  "devDependencies": {
    "json-server": "^0.14.0"
  },
  "bugs": {
    "url": "https://github.com/yourgithub/yourrepo"
  },
  "homepage": "https://github.com/yourgithubid/yourrepo
}

localhost:3000으로 들어가면

Congrats!
You're successfully running JSON Server
。٩(ˊᗜˋ)و✧*。
Resources

	/posts 3x
    /comments 1x
    /profile object

To access and modify resources, you can use any HTTP method:
GET POST PUT PATCH DELETE OPTIONS
undefined
Documentation
README

해당 문구가 뜨면 정상 서버 작동한 것이다.

상단 네비 My JSON Server 이동한다.

  1. 깃헙 레포 만들고

db.json 생성 및 작성

예시)

{
  "posts": [
    {
      "id": type Number
      "title": "word",
      "content": "word",
      "price": "whatever"
    },
    {
      "id": type Number
      "title": "word",
      "content": "word",
      "price": "whatever"
    },
    {
      "id": type Number
      "title": "word",
      "content": "word",
      "price": "whatever"
    }
  ],
  "comments": [{ "id": number, "body": "some comment", "postId": 1 }],
  "profile": { "name": "typicode" }
}
  1. 깃헙계정 / 레포 이름으로 jsonserver 생성
    https://my-json-server.typicode.com/yourgithubid/yourrepo

0개의 댓글