Free Json api 만들기

00_8_3·2020년 9월 20일
0

무료 json api 만들기

출처 : https://this-programmer.tistory.com/entry/JSON%EC%9D%84-%ED%85%8C%EC%8A%A4%ED%8A%B8%ED%95%98%EB%8A%94-%EA%B0%80%EC%9E%A5-%EC%A2%8B%EC%9D%80-%EB%B0%A9%EB%B2%95-POSTMAN%EA%B3%BC-JSON-Placeholder

https://this-programmer.tistory.com/entry/JSON%EC%9D%84-%ED%85%8C%EC%8A%A4%ED%8A%B8%ED%95%98%EB%8A%94-%EA%B0%80%EC%9E%A5-%EC%A2%8B%EC%9D%80-%EB%B0%A9%EB%B2%95-POSTMAN%EA%B3%BC-JSON-Placeholder

포스트맨


npm json server 사용하기

1 폴더 만들고 npm init

2 npm i json-server --save-dev

3 server.js 작성

const jsonServer = require('json-server')
const server = jsonServer.create()
const path = require('path')
const router = jsonServer.router(path.join(__dirname, 'db.json'))
const middlewares = jsonServer.defaults()

// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares)

// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser)

server.use(router)

let port = 80;
server.listen(port, () => {
  console.log(`JSON Server is running, port(${port})`)
})

4 db.json 생성

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

5 서버실행

node server.js

6 기능

method path
전체목록조회 get /posts
추가 post /posts
삭제 delete /posts/:id
수정 put /posts/:id

출처 : https://min9nim.github.io/2018/10/json-server/

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


출처 : https://redux-advanced.vlpt.us/3/01.html

0개의 댓글