IT 한길

json-server (docker)

IT/기타2020. 6. 17. 13:32

1. docker image : vimagick/json-server

  (https://hub.docker.com/r/vimagick/json-server)

 

2. docker 에서 json-server 설치

   - docker-compose.yml

json-server:
  image: vimagick/json-server
  command: -H 0.0.0.0 -p 3000 -w db.json
  ports:
    - "3000:3000"
  volumes:
    - ./data:/data
  restart: always

   - docker-compose up -d 

[root@localhost ~]# docker-compose up -d
Creating root_json-server_1 ... done

 

   - http://아이피:3000 접속

 

그림. JSON Server

3. 테스트 (curl)

  -  data/db.json

[root@localhost ~]# curl http://localhost:3000/db
{
  "posts": [
    {
      "id": 1,
      "title": "json-server",
      "author": "typicode"
    }
  ],
  "comments": [
    {
      "id": 1,
      "body": "some comment",
      "postId": 1
    }
  ],
  "profile": {
    "name": "typicode"
  }

  - POST : 레코드 추가

[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"id":2,"title":"test","author":"tester"}'  http://localhost:3000/posts
HTTP/1.1 201 Created
X-Powered-By: Express
Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Access-Control-Expose-Headers: Location
Location: http://localhost:3000/posts/2
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Content-Length: 54
ETag: W/"36-/lxOJ9UlKz12ZxEx1xqB+hE3CK8"
Date: Wed, 17 Jun 2020 05:22:03 GMT
Connection: keep-alive

{
  "id": 2,
  "title": "test",
  "author": "tester"
}

  - PUT : 레코드 수정

[root@localhost ~]# curl -i -X PUT -H 'Content-Type: application/json' -d '{"title":"test1","author":"tester1"}'  http://localhost:3000/posts/2
HTTP/1.1 200 OK
X-Powered-By: Express
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Content-Length: 56
ETag: W/"38-hQWeLGNyekH8SKlDj7CMiHCakTA"
Date: Wed, 17 Jun 2020 05:25:09 GMT
Connection: keep-alive

{
  "title": "test1",
  "author": "tester1",
  "id": 2
}

  - DELETE : 레코드 삭제

[root@localhost ~]# curl -i -X DELETE http://localhost:3000/posts/2
HTTP/1.1 200 OK
X-Powered-By: Express
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Content-Length: 2
ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
Date: Wed, 17 Jun 2020 05:20:33 GMT
Connection: keep-alive

{}