json-server模擬REST API


一般情況下,一個APP的數據都需要等待接口人員開發完對應的接口才可以獲取到,這樣子的效率有點低。最好是我們可以自己模擬接口數據,進行頁面的數據填充,打通所有關節,之后等接口開發好了,改下接口地址就好了。

我們今天所要講的工具:json-server就能實現這樣的功能。

1、安裝json-server

首先我們的電腦要安裝有nodejs,然后使用npm命令來安裝:sudo npm install -g json-server(windows系統的不需要加sudo)

MeWifi:~ cjy$ npm install -g json-server
/usr/local/bin/json-server -> /usr/local/lib/node_modules/json-server/bin/index.js
- ms@0.7.1 node_modules/json-server/node_modules/compression/node_modules/ms
/usr/local/lib
└─┬ json-server@0.11.2 
  ├─┬ compression@1.7.0 
  │ ├── bytes@2.5.0 
  │ └── debug@2.6.8 
  ├── cors@2.8.4 
  ├─┬ express-urlrewrite@1.2.0 
  │ └─┬ path-to-regexp@1.7.0 
  │   └── isarray@0.0.1 
  ├── please-upgrade-node@1.0.1 
  ├─┬ update-notifier@1.0.3
  │ └─┬ latest-version@2.0.0
  │   └─┬ package-json@2.4.0
  │     └─┬ got@5.7.1
  │       └─┬ readable-stream@2.3.3 
  │         └── isarray@1.0.0 
  └─┬ yargs@6.6.0
    └─┬ read-pkg-up@1.0.1
      └─┬ read-pkg@1.1.0
        └─┬ normalize-package-data@2.4.0 
          └── hosted-git-info@2.5.0 

檢驗是否安裝成功,查看json-server配置項:json-server -h

MeWifi:~ cjy$ json-server -h
/usr/local/bin/json-server [options] <source>

選項:
  --config, -c               Path to config file    [默認值: "json-server.json"]
  --port, -p                 Set port                             [默認值: 3000]
  --host, -H                 Set host                        [默認值: "0.0.0.0"]
  --watch, -w                Watch file(s)                                [布爾]
  --routes, -r               Path to routes file
  --middlewares, -m          Paths to middleware files                    [數組]
  --static, -s               Set static files directory
  --read-only, --ro          Allow only GET requests                      [布爾]
  --no-cors, --nc            Disable Cross-Origin Resource Sharing        [布爾]
  --no-gzip, --ng            Disable GZIP Content-Encoding                [布爾]
  --snapshots, -S            Set snapshots directory               [默認值: "."]
  --delay, -d                Add delay to responses (ms)
  --id, -i                   Set database id property (e.g. _id)  [默認值: "id"]
  --foreignKeySuffix, --fks  Set foreign key suffix (e.g. _id as in post_id)
                                                                  [默認值: "Id"]
  --quiet, -q                Suppress log messages from output            [布爾]
  --help, -h                 顯示幫助信息                                 [布爾]
  --version, -v              顯示版本號                                   [布爾]

示例:
  /usr/local/bin/json-server db.json
  /usr/local/bin/json-server file.js
  /usr/local/bin/json-server
  http://example.com/db.json

https://github.com/typicode/json-server 

 

2、運行

在任意的目錄創建一個json文件,例如在json目錄創建data.json文件,寫入以下內容,並在json目錄下執行json-server data.json

{
    "list": {
        "news": [
            {
          "newsId": 1, "title": "標題1", "content": "私服大師傅放聲大哭" }, {
          "newsId": 2, "title": "標題2", "content": "怎么可能" } ] }, "detail": { "object": { "name": "我是大錘", "sex": 1, "isComplete": true } } }

成功的話會出現如下內容

MeWifi:~ cjy$ cd /工作/json/
MeWifi:json cjy$ json-server data.json

  \{^_^}/ hi!

  Loading data.json
  Done

  Resources
  http://localhost:3000/list
  http://localhost:3000/detail

  Home
  http://localhost:3000

  Type s + enter at any time to create a snapshot of the database

接下去就可以訪問了:

 

3、常用操作 

json-server支持GET, POST, PUT, PATCH 和 DELETE 方法,更提供了一系列的查詢方法,如limit,order等。

這里主要介紹下它的強大的過濾功能

示例數據源:

[
    {
        "newsId": 1,
        "title": "標題1",
        "content": "私服大師傅放聲大哭"
    },
    {
        "newsId": 2,
        "title": "標題2",
        "content": "怎么可能"
    }
]

屬性值(Filter)

使用 . 操作對象屬性值

//獲取content的長度為4的數據

 

排序(Sort)

使用 _sort 和 _order (默認使用升序(ASC))

//按照newsId降序排列

分割(Slice)

使用 _start 和 _end 或者 _limit 

//從下標0(第一個對象)開始,獲取1條數據

全文檢索

使用 q,在對象全部value中遍歷查找包含指定值的數據

//查找全部字段包含“可能”的數據

 

使用 _like 進行模糊查找 (支持正則表達式)

//查找content中含有“大哭”字樣的數據

還有一些操作,可以參考這篇文章:json-server說明


免責聲明!

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



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