Home >Web Front-end >JS Tutorial >JSON Server Example
This JSON server example is part of an article series that was rewritten in mid 2017 with up-to-date information and fresh examples.
The JSON Server is a popular tool for front-end developers for quickly setting up a fully fake REST API in less than a minute. You need to first install it via npm:<span>npm install -global json-server </span>
Next, save some data in a JSON file and name it db.json:
<span>{ </span> <span>"clients": [ </span> <span>{ </span> <span>"id": "59761c23b30d971669fb42ff", </span> <span>"isActive": true, </span> <span>"age": 36, </span> <span>"name": "Dunlap Hubbard", </span> <span>"gender": "male", </span> <span>"company": "CEDWARD", </span> <span>"email": "dunlaphubbard@cedward.com", </span> <span>"phone": "+1 (890) 543-2508", </span> <span>"address": "169 Rutledge Street, Konterra, Northern Mariana Islands, 8551" </span> <span>}, </span> <span>{ </span> <span>"id": "59761c233d8d0f92a6b0570d", </span> <span>"isActive": true, </span> <span>"age": 24, </span> <span>"name": "Kirsten Sellers", </span> <span>"gender": "female", </span> <span>"company": "EMERGENT", </span> <span>"email": "kirstensellers@emergent.com", </span> <span>"phone": "+1 (831) 564-2190", </span> <span>"address": "886 Gallatin Place, Fannett, Arkansas, 4656" </span> <span>}, </span> <span>{ </span> <span>"id": "59761c23fcb6254b1a06dad5", </span> <span>"isActive": true, </span> <span>"age": 30, </span> <span>"name": "Acosta Robbins", </span> <span>"gender": "male", </span> <span>"company": "ORGANICA", </span> <span>"email": "acostarobbins@organica.com", </span> <span>"phone": "+1 (882) 441-3367", </span> <span>"address": "697 Linden Boulevard, Sattley, Idaho, 1035" </span> <span>} </span> <span>] </span><span>} </span>
Finally, start the server with the following command:
json-server <span>--watch src/db.json </span>
You can now access the simple REST API with a suitable client. For now, a modern browser like Chrome, Firefox or Safari will do. Open http://localhost:3000/clients and you will see your entire miniature database in JSON format. You can view items by id by using the request format http://localhost:3000/clients/{id}. For example, opening http://localhost:3000/clients/59761c233d8d0f92a6b0570d will yield:
<span>{ </span> <span>"id": "59761c233d8d0f92a6b0570d", </span> <span>"isActive": true, </span> <span>"age": 24, </span> <span>"name": "Kirsten Sellers", </span> <span>"gender": "female", </span> <span>"company": "EMERGENT", </span> <span>"email": "kirstensellers@emergent.com", </span> <span>"phone": "+1 (831) 564-2190", </span> <span>"address": "886 Gallatin Place, Fannett, Arkansas, 4656" </span><span>} </span>
To learn more about the JSON server, check out the tutorial Mock REST APIs Using json-server
Also: See more JSON examples.
Here are the other examples in this series:JSON Server is a simple tool primarily used for setting up a fake REST API for development purposes. It allows developers to prototype and develop applications without having to set up a complex backend. This is particularly useful when you want to quickly test your front-end code with a mock backend. It uses a JSON file to create the database and provides all the standard REST API endpoints out of the box.
JSON Server is a Node.js module, and it can be installed using npm (Node Package Manager). You can install it globally on your system by running the command npm install -g json-server in your terminal or command prompt. Once installed, you can start the server with the command json-server --watch db.json, where db.json is your database file.
JSON Server allows you to define custom routes by creating a routes.json file. In this file, you can map routes to different JSON objects. For example, if you want to map /api/posts to /posts, you would define it as {"/api/posts": "/posts"} in your routes.json file. Then, you can start the server with the routes file by running json-server --watch db.json --routes routes.json.
While JSON Server is a powerful tool for prototyping and development, it is not recommended for production use. It lacks the security and performance optimizations necessary for a production environment. For production, you should use a proper database and server setup.
You can add data to your JSON Server by modifying the db.json file. This file acts as your database, and each key in the JSON object corresponds to a different resource. For example, if you want to add a new post, you would add a new object to the posts array in your db.json file.
JSON Server supports filtering data using query parameters. For example, if you want to get all posts with the title “Hello World”, you would send a GET request to /posts?title=Hello World. This will return all posts where the title is “Hello World”.
Yes, JSON Server is language-agnostic and can be used with any programming language that can send HTTP requests. This includes JavaScript, Python, Ruby, Java, and many others.
JSON Server supports pagination using the _page and _limit query parameters. For example, if you want to get the first 10 posts, you would send a GET request to /posts?_page=1&_limit=10. This will return the first 10 posts.
Yes, JSON Server supports sorting data using the _sort and _order query parameters. For example, if you want to get posts sorted by title in ascending order, you would send a GET request to /posts?_sort=title&_order=asc.
You can update data in JSON Server by sending a PUT or PATCH request to the resource’s URL. For example, if you want to update the title of a post, you would send a PUT or PATCH request to /posts/1, where 1 is the id of the post, with the new title in the request body.
The above is the detailed content of JSON Server Example. For more information, please follow other related articles on the PHP Chinese website!