### Install JSON Server with npm Source: https://github.com/typicode/json-server/blob/main/README.md Install JSON Server using npm. This is the first step to setting up your mock API. ```shell npm install json-server ``` -------------------------------- ### Start JSON Server Source: https://github.com/typicode/json-server/blob/main/README.md Launch the JSON Server using npx with your db.json file. This command starts the mock API server. ```bash npx json-server db.json ``` -------------------------------- ### Access API Endpoint with curl Source: https://github.com/typicode/json-server/blob/main/README.md Example of how to fetch data from a specific endpoint using curl after starting the JSON Server. ```bash curl http://localhost:3000/posts/1 ``` -------------------------------- ### Serving Additional Static Directories Source: https://github.com/typicode/json-server/blob/main/README.md Command-line examples for serving static files from additional directories using the `-s` flag. ```bash json-server db.json -s ./static json-server db.json -s ./static -s ./node_modules ``` -------------------------------- ### Sorting Examples Source: https://github.com/typicode/json-server/blob/main/README.md Shows how to sort resources by one or multiple fields, including descending order. ```http GET /posts?_sort=title GET /posts?_sort=-views GET /posts?_sort=author.name,-views ``` -------------------------------- ### Pagination Example Source: https://github.com/typicode/json-server/blob/main/README.md Demonstrates how to paginate results using `_page` and `_per_page` query parameters. Defaults to 10 items per page if not specified. ```http GET /posts?_page=1&_per_page=25 ``` -------------------------------- ### JSON Server Response Example Source: https://github.com/typicode/json-server/blob/main/README.md The expected JSON response when accessing the /posts/1 endpoint. ```json { "id": "1", "title": "a title", "views": 100 } ``` -------------------------------- ### db.json5 Example Source: https://github.com/typicode/json-server/blob/main/README.md An alternative format for your database file using JSON5, which offers more flexibility in syntax. ```json5 { posts: [ { id: "1", title: "a title", views: 100 }, { id: "2", title: "another title", views: 200 }, ], comments: [ { id: "1", text: "a comment about post 1", postId: "1" }, { id: "2", text: "another comment about post 1", postId: "1" }, ], profile: { name: "typicode", }, } ``` -------------------------------- ### Query Conditions Examples Source: https://github.com/typicode/json-server/blob/main/README.md Illustrates various operators for filtering data, including equality, range, inclusion, and string matching. ```http GET /posts?views:gt=100 GET /posts?title:eq=Hello GET /posts?id:in=1,2,3 GET /posts?author.name:eq=typicode GET /posts?title:contains=hello GET /posts?title:startsWith=Hello GET /posts?title:endsWith=world ``` -------------------------------- ### Embedding Related Resources Source: https://github.com/typicode/json-server/blob/main/README.md Examples of embedding related resources using the `_embed` query parameter. ```http GET /posts?_embed=comments GET /comments?_embed=post ``` -------------------------------- ### Basic Query Capabilities Source: https://github.com/typicode/json-server/blob/main/README.md Demonstrates various ways to query resources using query parameters for filtering, sorting, pagination, and embedding. ```http GET /posts?views:gt=100 # Filter by condition GET /posts?_sort=-views # Sort by field (descending) GET /posts?_page=1&_per_page=10 # Pagination GET /posts?_embed=comments # Include relations GET /posts?_where={"or":[...]} # Complex queries ``` -------------------------------- ### Create db.json for JSON Server Source: https://github.com/typicode/json-server/blob/main/README.md Define your API resources and data in a db.json file. This file serves as the database for your mock API. ```json { "$schema": "./node_modules/json-server/schema.json", "posts": [ { "id": "1", "title": "a title", "views": 100 }, { "id": "2", "title": "another title", "views": 200 } ], "comments": [ { "id": "1", "text": "a comment about post 1", "postId": "1" }, { "id": "2", "text": "another comment about post 1", "postId": "1" } ], "profile": { "name": "typicode" } } ``` -------------------------------- ### Object Resource Routes Source: https://github.com/typicode/json-server/blob/main/README.md Standard RESTful routes for singular object resources like 'profile'. ```http GET /profile PUT /profile PATCH /profile ``` -------------------------------- ### Static Files Serving Source: https://github.com/typicode/json-server/blob/main/README.md JSON Server can automatically serve static files from a specified directory. ```APIDOC ## Static Files JSON Server automatically serves files from the `./public` directory. To serve additional static directories: ```bash json-server db.json -s ./static json-server db.json -s ./static -s ./node_modules ``` Static files are served with standard MIME types and can include HTML, CSS, JavaScript, images, and other assets. ``` -------------------------------- ### Delete Dependents Source: https://github.com/typicode/json-server/blob/main/README.md Demonstrates how to delete a resource and its dependents using the `_dependent` query parameter. ```http DELETE /posts/1?_dependent=comments ``` -------------------------------- ### Query Parameters - Pagination Source: https://github.com/typicode/json-server/blob/main/README.md Implement pagination for resources using page number and items per page. ```APIDOC ## Query params - Pagination ```http GET /posts?_page=1&_per_page=25 ``` **Response:** ```json { "first": 1, "prev": null, "next": 2, "last": 4, "pages": 4, "items": 100, "data": [ { "id": "1", "title": "...", "views": 100 }, { "id": "2", "title": "...", "views": 200 } ] } ``` **Notes:** - `_per_page` defaults to `10` if not specified - Invalid `_page` or `_per_page` values are automatically normalized to valid ranges ``` -------------------------------- ### Array Resource Routes Source: https://github.com/typicode/json-server/blob/main/README.md Standard RESTful routes for array-based resources like 'posts'. ```http GET /posts GET /posts/:id POST /posts PUT /posts/:id PATCH /posts/:id DELETE /posts/:id ``` -------------------------------- ### Complex Filtering with `_where` Source: https://github.com/typicode/json-server/blob/main/README.md Shows how to use the `_where` query parameter for complex filtering logic, overriding normal query parameters when valid. ```http GET /posts?_where={"or":[{"views":{"gt":100}},{"author":{"name":{"lt":"m"}}}]} ``` -------------------------------- ### Array Resources Source: https://github.com/typicode/json-server/blob/main/README.md Standard RESTful endpoints for array-based resources like posts and comments. ```APIDOC ## Array Resources For array resources like `posts` and `comments`: ```http GET /posts GET /posts/:id POST /posts PUT /posts/:id PATCH /posts/:id DELETE /posts/:id ``` ``` -------------------------------- ### Object Resources Source: https://github.com/typicode/json-server/blob/main/README.md Endpoints for singular object resources such as a user profile. ```APIDOC ## Object Resources For singular object resources like `profile`: ```http GET /profile PUT /profile PATCH /profile ``` ``` -------------------------------- ### Query Parameters - Conditions Source: https://github.com/typicode/json-server/blob/main/README.md Filter resources using field-based conditions with various operators. ```APIDOC ## Query params - Conditions Use `field:operator=value`. Operators: - no operator -> `eq` (equal) - `lt` less than, `lte` less than or equal - `gt` greater than, `gte` greater than or equal - `eq` equal, `ne` not equal - `in` included in comma-separated list - `contains` string contains (case-insensitive) - `startsWith` string starts with (case-insensitive) - `endsWith` string ends with (case-insensitive) Examples: ```http GET /posts?views:gt=100 GET /posts?title:eq=Hello GET /posts?id:in=1,2,3 GET /posts?author.name:eq=typicode GET /posts?title:contains=hello GET /posts?title:startsWith=Hello GET /posts?title:endsWith=world ``` ``` -------------------------------- ### Query Parameters - Embed Source: https://github.com/typicode/json-server/blob/main/README.md Include related resources within the response using the `_embed` query parameter. ```APIDOC ## Query params - Embed ```http GET /posts?_embed=comments GET /comments?_embed=post ``` ``` -------------------------------- ### Query Parameters - Sort Source: https://github.com/typicode/json-server/blob/main/README.md Sort the results by one or more fields, with options for ascending and descending order. ```APIDOC ## Query params - Sort ```http GET /posts?_sort=title GET /posts?_sort=-views GET /posts?_sort=author.name,-views ``` ``` -------------------------------- ### Pagination Response Structure Source: https://github.com/typicode/json-server/blob/main/README.md The structure of a paginated response, including navigation links and item counts. ```json { "first": 1, "prev": null, "next": 2, "last": 4, "pages": 4, "items": 100, "data": [ { "id": "1", "title": "...", "views": 100 }, { "id": "2", "title": "...", "views": 200 } ] } ``` -------------------------------- ### Query Parameters - Complex Filter with _where Source: https://github.com/typicode/json-server/blob/main/README.md Utilize the `_where` parameter for complex filtering logic using JSON objects. ```APIDOC ## Complex filter with `_where` `_where` accepts a JSON object and overrides normal query params when valid. ```http GET /posts?_where={"or":[{"views":{"gt":100}},{"author":{"name":{"lt":"m"}}}]} ``` ``` -------------------------------- ### Delete Dependents Source: https://github.com/typicode/json-server/blob/main/README.md Delete a resource and its associated dependent resources. ```APIDOC ## Delete dependents ```http DELETE /posts/1?_dependent=comments ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.