GithubHelp home page GithubHelp logo

json-mock-server-sample's Introduction

準備

npm installor yarn install json-serverは上記の場合PATHが通っていないのでコマンドが見つからない。 面倒であればjson-serverのみグローバルインストールするか、パスをちゃんと通す

サンプルデータモデル

db.jsonに格納されているサンプルデータモデル

{
  "movies": [
    { "id": <int>, "name": <String>, "director": <String>, "rating": <float> },
    ...
  ]
}

上記はfakerを利用して、faker-movies-data-generartor.js$ node faker-movies-data-generartor > db.jsonを実行して作成

起動

$ json-server --watch db.jsonで起動

JSONコール方法

詳しいことはjson-serverのドキュメント参照[https://github.com/typicode/json-server]

データに対するCURD

  • C
$ curl -X POST -H "Content-Type: application/json" -d '{
  "id": 3,
  "name": "Inception",
  "director": "Christopher Nolan",
  "rating": 9.0
}' "http://localhost:3000/movies"
  • U PUTメソッドを使えばできる。試してない。

  • R

$ curl -X GET "http://localhost:3000/movies" -> get all data
$ curl -X GET "http://localhost:3000/movies/3" -> get id is 3.
  • D DELETEメソッドでできる。試してない。

フィルター

$ curl -X GET "http://localhost:3000/movies?name=Casablanca"
$ curl -X GET "http://localhost:3000/movies?name=Casablanca&id=2"

レンジ指定

$ curl -X GET "http://localhost:3000/movies?rating_gte=9"
$ curl -X GET "http://localhost:3000/movies?rating_gte=5&rating_lte=7"

ページング

$ curl -X GET "http://localhost:3000/movies?_page=3"

21 - 30件目のデータが戻る。(デフォルトページサイズは10件)

並び替え

$ curl -X GET "http://localhost:3000/movies?_sort=name&_order=DESC"

==========================================================

Author: Takahiro Oshima ([email protected])
License: MIT License
Created: 2018-01-04

==========================================================

json-mock-server-sample's People

Contributors

smockoro avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.