天天看點

JSONPlaceholder

title: 測試

date: 2019-02-18 09:20:45

tags: 不知道

一個用于快速搭建RESTful API的接口 JSONPlaceholder

位址

Json-Server ,個人搭建一個 通路

GITHub

你可以建立一個檔案夾,執行

`npm install -g json-server

`

本地執行個體化一個項目

npm install json-server --save

我們的package.json
"name": "jsonserver",
 "version": "1.0.0",
 "description": "test restful api",
 "main": "index.js",
 "scripts": {
   "json:server": "json-server --watch db.json",
   "json:server:remote": "json-server http://jsonplaceholder.typicode.com/db"
 },
 "author": "",
 "license": "ISC",
 "dependencies": {
   "json-server": "^0.12.2"
 }
}           

此時你建立一個db.json檔案,然後輸入對應的json

{
  "users": [
    {
      "name": "Henry",
      "phone": "333-444-555",
      "id": 1
    },
    {
      "name": "adfasdf",
      "phone": "fasdfasfd",
      "email": "asdfasfd",
      "id": 3
    }
  ],
  "xxx": [
    {
      "aa":“abab”
    }
  ]
}           

運作

npm run json:server

,會執行對應的db.json 資料,給你

在浏覽器可以直接通路,會擷取對應的接口資料

你們可以使用postman去測試調用,各種方法的使用具體看官網

// 擷取所有使用者資訊
http://localhost:3000/users

// 擷取id為1的使用者資訊
http://localhost:3000/users/1

// 擷取公司的所有資訊
http://localhost:3000/companies

// 擷取單個公司的資訊
http://localhost:3000/companies/1

// 擷取所有公司id為3的使用者
http://localhost:3000/companies/3/users

// 根據公司名字擷取資訊
http://localhost:3000/companies?name=Microsoft

// 根據多個名字擷取公司資訊
http://localhost:3000/companies?name=Microsoft&name=Apple

// 擷取一頁中隻有兩條資料
http://localhost:3000/companies?_page=1&_limit=2

// 升序排序 asc升序 desc降序
http://localhost:3000/companies?_sort=name&_order=asc

// 擷取年齡30及以上的
http://localhost:3000/users?age_gte=30

// 擷取年齡在30到40之間
http://localhost:3000/users?age_gte=30&age_lte=40

// 搜尋使用者資訊
http://localhost:3000/users?q=h