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.
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.
$ json-server --watch db.json
\{^_^}/ hi!
Loading db.json
Done
Resources
https://localhost:3000/employees
Home
https://localhost:3000
Type s + enter at any time to create a snapshot of the database
Watching...
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)
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.
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.
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-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.json
Loading routes.json
Done
Resources
https://localhost:7000/employees
Other routes
/employees/list -> /employees
/employees/get/:id -> /employees/:id
/employees/create -> /employees
/employees/update/:id -> /employees/:id
/employees/delete/:id -> /employees/:id
Home
https://localhost:7000
Type s + enter at any time to create a snapshot of the database
Watching...
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.
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.
原题链接:#137 Single Number II
要求:
给定一个整型数组,其中除了一个元素之外,每个元素都出现三次。找出这个元素
注意:算法的时间复杂度应为O(n),最好不使用额外的内存空间
难度:中等
分析:
与#136类似,都是考察位运算。不过出现两次的可以使用异或运算的特性 n XOR n = 0, n XOR 0 = n,即某一
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, det