windows搭建json-server快速方法


JSON-Server 是一個 Node 模塊,運行 Express 服務器,你可以指定一個 json 文件作為 api 的數據源。

 

一、下載並安裝node.js

安裝完后輸入 node --version 檢查是否安裝成功

 

二、安裝json-server

 

1.新建項目 mkdir json-demo && cd json-demo 

 

2.初始化項目  npm init  或  npm init --yes 

 

前者需都按回車,

 

 

3.安裝  npm install json-server --save   或    npm install --save-dev json-server 

 

4.在項目根目錄(C:\Users\Administrator\json-demo)下,新建一個 JSON 文件db.json

{
  "users": [
    {
      "name": "tt",
      "phone": "123456789",
      "email": "111@qq.com",
      "age": "20",
      "id": 1,
      "companyId": 1
    },
    {
      "name": "dede",
      "phone": "123456789",
      "email": "222@qq.com",
      "age": "30",
      "id": 2,
      "companyId": 2
    },
    {
      "name": "wf",
      "phone": "123456789",
      "email": "333@qq.com",
      "age": "23",
      "id": 3,
      "companyId": 3
    },
    {
      "name": "mj",
      "phone": "123456789",
      "email": "444@qq.com",
      "age": "45",
      "id": 4,
      "companyId": 3
    }
  ],
  "companies": [
    {
      "id": 1,
      "name": "Apple",
      "description": "Apple lalala!"
    },
    {
      "id": 2,
      "name": "Microsoft",
      "description": "Microsoft lalala!"
    },
    {
      "id": 3,
      "name": "Google",
      "description": "Google lalala!"
    }
  ]
}

 

 

5. 把db.json文件托管成一個 web 服務  json-server --watch --port 53000 db.json 

 

6.打開瀏覽器輸入  http://localhost:53000/companies訪問成功如下

 

 

7. 如果關閉了cmd窗口,再次打開可以再次輸入 json-server --watch --port 53000 db.json

  

8. 查看相關內容

// 獲取所有用戶信息
http://localhost:53000/users

// 獲取id為1的用戶信息
http://localhost:53000/users/1

// 獲取公司的所有信息
http://localhost:53000/companies

// 獲取單個公司的信息
http://localhost:53000/companies/1

// 獲取所有公司id為3的用戶
http://localhost:53000/companies/3/users

// 根據公司名字獲取信息
http://localhost:3000/companies?name=Microsoft

// 根據多個名字獲取公司信息
http://localhost:53000/companies?name=Microsoft&name=Apple

// 獲取一頁中只有兩條數據
http://localhost:53000/companies?_page=1&_limit=2

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

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

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

// 搜索用戶信息
http://localhost:53000/users?q=h  h指的是查詢的首字母

 

參考

1 json-server快速“偽造”后台接口

一分鍾內搭建全web的API接口神器json-server詳解

使用json-server快速搭建本地數據接口

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM