支持json的數據庫,JSON服務器(json-server)

 2023-11-19 阅读 22 评论 0

摘要:Today we will look into a very handy tool json-server, which can give you a mock rest json server in a minute. 今天,我們將研究一個非常方便的工具json-server,它可以在一分鐘內為您提供一個模擬剩余的json服務器。 支持json的數據庫?In a regular ent

Today we will look into a very handy tool json-server, which can give you a mock rest json server in a minute.

今天,我們將研究一個非常方便的工具json-server,它可以在一分鐘內為您提供一個模擬剩余的json服務器。

支持json的數據庫?In a regular enterprise application, you work with many teams and third party APIs. Imagine you have to call a third party restful web service that will get you JSON data to work on. You are in a tight schedule, so you can’t wait for them to finish their work and then start your own. If you wish to have a mockup Rest Web service in place to get the demo data for you, then json-server is the tool you are looking for.

在常規企業應用程序中,您需要使用許多團隊和第三方API。 假設您必須調用第三方靜態Web服務 ,該服務將使您可以使用JSON數據。 您的時間安排很緊,所以您不能等他們完成工作然后再開始自己的工作。 如果您希望有一個原型Rest Web服務來為您獲取演示數據,那么json-server是您要尋找的工具。

JSON服務器 (JSON Server)

JSON Server is a Node Module that you can use to create demo rest json webservice in less than a minute. All you need is a JSON file for sample data.

如何更新服務器端的json文件、 JSON Server是一個節點模塊,可用于在不到一分鐘的時間內創建演示剩余json網絡服務。 您只需要一個JSON文件即可獲取示例數據。

安裝JSON服務器 (Installing JSON Server)

You should have NPM installed on your machine. If not, then refer this post to install NPM.

您應該在計算機上安裝NPM。 如果沒有,請參考這篇文章來安裝NPM 。

apijson教程。Below shows the one liner command to install json-server with output on my machine.

下面顯示了一個在我的機器上安裝json-server及其輸出的內襯命令。

$ npm install -g json-server
npm WARN deprecated graceful-fs@3.0.8: graceful-fs v3.0.0 and before will fail on node releases >= v7.0. Please update to graceful-fs@^4.0.0 as soon as possible. Use 'npm ls graceful-fs' to find it in the tree.
/usr/local/bin/json-server -> /usr/local/lib/node_modules/json-server/bin/index.js
- bytes@2.3.0 node_modules/json-server/node_modules/raw-body/node_modules/bytes
/usr/local/lib
└─┬ json-server@0.8.10├─┬ body-parser@1.15.1│ └── bytes@2.3.0├─┬ compression@1.6.1│ └── bytes@2.2.0├─┬ lowdb@0.10.3│ └─┬ steno@0.4.4│   └── graceful-fs@4.1.4├─┬ update-notifier@0.5.0│ └─┬ configstore@1.4.0│   ├── graceful-fs@4.1.4│   └─┬ write-file-atomic@1.1.4│     └── graceful-fs@4.1.4└─┬ yargs@4.7.0├─┬ pkg-conf@1.1.2│ └─┬ load-json-file@1.1.0│   └── graceful-fs@4.1.4└─┬ read-pkg-up@1.0.1└─┬ read-pkg@1.1.0└─┬ path-type@1.1.0└── graceful-fs@4.1.4$

檢查json服務器版本和選項 (Checking json-server version and options)

$ json-server -v
0.8.10$ json-server -help
/usr/local/bin/json-server [options] <source>Options:--config, -c       Path to config file           [default: "json-server.json"]--port, -p         Set port                                    [default: 3000]--host, -H         Set host                               [default: "0.0.0.0"]--watch, -w        Watch file(s)                                     [boolean]--routes, -r       Path to routes file--static, -s       Set static files directory--read-only, --ro  Allow only GET requests                           [boolean]--no-cors, --nc    Disable Cross-Origin Resource Sharing             [boolean]--no-gzip, --ng    Disable GZIP Content-Encoding                     [boolean]--snapshots, -S    Set snapshots directory                      [default: "."]--delay, -d        Add delay to responses (ms)--id, -i           Set database id property (e.g. _id)         [default: "id"]--quiet, -q        Suppress log messages from output                 [boolean]$

運行JSON服務器 (Run JSON Server)

Now it’s time to start our json-server. Below is a sample file with my employees json data.

Java json。 現在是時候啟動我們的json服務器了。 以下是包含我的員工json數據的示例文件。

{"employees": [{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "David","salary": "5000","id": 2}]
}

Important point here is the name of array i.e employees. JSON server will create the REST APIs based on this. Let’s start our json-server with above file.

這里的重點是數組的名稱,即employees。 JSON服務器將基于此創建REST API。 讓我們用上述文件啟動json服務器。

$ json-server --watch db.json\{^_^}/ hi!Loading db.jsonDoneResourceshttps://localhost:3000/employeesHomehttps://localhost:3000Type s + enter at any time to create a snapshot of the databaseWatching...

json sql、Don’t close this terminal, otherwise it will kill the json-server. Below are the sample CRUD requests and responses.

不要關閉此終端,否則它將殺死json-server。 以下是示例CRUD請求和響應。

JSON Server GET –閱讀所有員工 (JSON Server GET – Read All Employees)

$ curl -X GET -H "Content-Type: application/json"  "https://localhost:3000/employees"
[{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "David","salary": "5000","id": 2}
]
$

根據json-server的ID獲取員工 (Get Employee based on ID from json-server)

$ curl -X GET -H "Content-Type: application/json"  "https://localhost:3000/employees/1"
{"id": 1,"name": "Pankaj","salary": "10000"
}
$

JSON服務器POST-創建員工 (JSON Server POST – Create an Employee)

$ curl -X POST -H "Content-Type: application/json" -d '{"name": "Lisa","salary": "2000"}' "https://localhost:3000/employees"
{"name": "Lisa","salary": 2000,"id": 3
}
$

JSON Server PUT –更新員工數據 (JSON Server PUT – Update Employee Data)

$ curl -XPUT -H "Content-Type: application/json" -d '{"name": "Lisa", "salary": "8000"}' "https://localhost:3000/employees/3"
{"name": "Lisa","salary": 8000,"id": 3
}
$

JSON服務器DELETE –刪除員工 (JSON Server DELETE – Delete an Employee)

$ curl -X DELETE -H "Content-Type: application/json"  "https://localhost:3000/employees/2"
{}
$ curl -GET -H "Content-Type: application/json"  "https://localhost:3000/employees"
[{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "Lisa","salary": 8000,"id": 3}
]
$

As you can see that with a simple JSON, json-server creates demo APIs for us to use. Note that all the PUT, POST, DELETE requests are getting saved into db.json file.

怎么安裝json。 如您所見,json-server通過簡單的JSON創建了供我們使用的演示API。 請注意,所有的PUT,POST,DELETE請求都將保存到db.json文件中。

Now the URIs for GET and DELETE are same, similarly it’s same for POST and PUT requests. Well, we can create our custom URIs too with a simple mapping file.

現在,GET和DELETE的URI相同,而POST和PUT請求的URI相同。 好了,我們也可以使用一個簡單的映射文件來創建自定義URI。

json-server自定義路由 (json-server custom routes)

mysql json?Create a file with custom routes for our json-server to use.

創建一個具有自定義路由的文件,供我們的json服務器使用。

routes.json

routes.json

{"/employees/list": "/employees","/employees/get/:id": "/employees/:id","/employees/create": "/employees","/employees/update/:id": "/employees/:id","/employees/delete/:id": "/employees/:id"
}

We can also change the json-server port and simulate like a third party API, just change the base URL when the real service is ready and you will be good to go.

我們還可以更改json服務器端口并像第三方API一樣進行仿真,只需在實際服務就緒時更改基本URL,您就可以使用了。

Now start the JSON server again as shown below.

現在,如下所示再次啟動JSON服務器。

$ json-server --port 7000 --routes routes.json --watch db.json
(node:60899) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.\{^_^}/ hi!Loading db.jsonLoading routes.jsonDoneResourceshttps://localhost:7000/employeesOther routes/employees/list -> /employees/employees/get/:id -> /employees/:id/employees/create -> /employees/employees/update/:id -> /employees/:id/employees/delete/:id -> /employees/:idHomehttps://localhost:7000Type s + enter at any time to create a snapshot of the databaseWatching...

It’s showing the custom routes defined by us.

它顯示了我們定義的自定義路線。

具有自定義路由的json-server示例 (json-server example with custom routes)

Below is the example of some of the commands and their output with custom routes.

以下是一些命令及其帶有自定義路由的輸出示例。

$ curl -X GET -H "Content-Type: application/json"  "https://localhost:7000/employees/list"
[{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "Lisa","salary": 8000,"id": 3}
]$ curl -X GET -H "Content-Type: application/json"  "https://localhost:7000/employees/get/1"
{"id": 1,"name": "Pankaj","salary": "10000"
}$ curl -X POST -H "Content-Type: application/json" -d '{"name": "Lisa","salary": "2000"}' "https://localhost:7000/employees/create"
{"name": "Lisa","salary": 2000,"id": 4
}$ curl -XPUT -H "Content-Type: application/json" -d '{"name": "Lisa", "salary": "8000"}' "https://localhost:7000/emloyees/update/4"
{"name": "Lisa","salary": 8000,"id": 4
}$ curl -XDELETE -H "Content-Type: application/json"  "https://localhost:7000/employees/delete/4"
{}$ curl -GET -H "Content-Type: application/json"  "https://localhost:7000/employees/list"
[{"id": 1,"name": "Pankaj","salary": "10000"},{"name": "Lisa","salary": 8000,"id": 3}
]
$

JSON server provides some other useful options such as sorting, searching and pagination. That’s all for json-server, it’s my go to tool whenever I need to create demo Rest JSON APIs.

JSON服務器提供了一些其他有用的選項,例如排序,搜索和分頁。 這就是json服務器的全部內容,當我需要創建演示Rest JSON API時,這就是我要使用的工具。

Reference: json-server GitHub

參考 : json-server GitHub

翻譯自: https://www.journaldev.com/10660/json-server

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/182935.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息