{"id":22358338,"url":"https://github.com/thedvlprs/json_server_lib","last_synced_at":"2025-03-26T13:43:41.535Z","repository":{"id":43933361,"uuid":"273230130","full_name":"thedvlprs/json_server_lib","owner":"thedvlprs","description":":tada: JSON Server tutorial introduces the JavaScript json-server library, which can be used to create fake REST API.","archived":false,"fork":false,"pushed_at":"2025-03-08T19:12:35.000Z","size":1767,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T20:20:30.626Z","etag":null,"topics":["insomnia","json-server-lib","rest-api","simple-rest-client"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thedvlprs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-18T12:19:16.000Z","updated_at":"2025-03-08T19:12:32.000Z","dependencies_parsed_at":"2024-01-11T06:42:21.597Z","dependency_job_id":"5a7da34c-6bb9-4114-94f1-909836381842","html_url":"https://github.com/thedvlprs/json_server_lib","commit_stats":null,"previous_names":["thedvlprs/json_server_lib"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_server_lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_server_lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_server_lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_server_lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thedvlprs","download_url":"https://codeload.github.com/thedvlprs/json_server_lib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245667554,"owners_count":20652982,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["insomnia","json-server-lib","rest-api","simple-rest-client"],"created_at":"2024-12-04T15:14:19.503Z","updated_at":"2025-03-26T13:43:41.491Z","avatar_url":"https://github.com/thedvlprs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON Server Tutorial\n\n**JSON Server** tutorial introduces the JavaScript `json-server` library, which can be used to create **fake REST API**.\n\n## JSON server\n\nThe *json-server* is a JavaScript library to create testing REST API.\n\n## Tools\n\n* **VSC**\n* **Simple REST Client** *VSC extension* (Easy to examine your API's responses directly within VSC. Send requests to verify your API)\n\n![](demo-2.png)\n\nOr alternatively\n\n* **Insomnia**\n![](demo-1.png)\n\n## JSON Server installation\n\n\nFirst, we create a project directory an install the `json-server` module.\n\n`$ mkdir json-server-lib`\n\n`$ cd json-server-lib`\n\n`$ npm init -y`\n\n`$ npm i -g json-server`\n\nThe JSON server module is installed globally with `npm`.\n\n`$ npm install axios`\n\nIn addition, we install the `axios` module, which is a promise-based **JavaScript HTTP client**.\n\n`$ cat package.json`\n\n```json\n{\n  \"name\": \"json-server-lib\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"index.js\",\n  \"dependencies\": {\n    \"axios\": \"^0.18.0\"\n  },\n  \"devDependencies\": {},\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" \u0026\u0026 exit 1\"\n  },\n  \"keywords\": [],\n  \"author\": \"\",\n  \"license\": \"ISC\"\n}\n```\n\nThis is our `package.json` file.\n\n## JSON test data\n\nWe have some **JSON test data**:\n\n`users.json`\n\n```json\n{\n  \"users\": [\n    {\n      \"id\": 1,\n      \"first_name\": \"Robert\",\n      \"last_name\": \"Schwartz\",\n      \"email\": \"rob23@gmail.com\"\n    },\n    {\n      \"id\": 2,\n      \"first_name\": \"Lucy\",\n      \"last_name\": \"Ballmer\",\n      \"email\": \"lucyb56@gmail.com\"\n    },\n    {\n      \"id\": 3,\n      \"first_name\": \"Anna\",\n      \"last_name\": \"Smith\",\n      \"email\": \"annasmith23@gmail.com\"\n    },\n    {\n      \"id\": 4,\n      \"first_name\": \"Robert\",\n      \"last_name\": \"Brown\",\n      \"email\": \"bobbrown432@yahoo.com\"\n    },\n    {\n      \"id\": 5,\n      \"first_name\": \"Roger\",\n      \"last_name\": \"Bacon\",\n      \"email\": \"rogerbacon12@yahoo.com\"\n    }\n  ]\n} \n```\n   \n## Starting JSON server\n\nThe JSON server is started with the `json-server`, which we have installed globally.\n\n`$ json-server --watch users.json `   \n\nThe `--watch` command is used to specify the data for the server.\n\n`$ curl localhost:3000/users/3/`\n\n```json\n{\n  \"id\": 3,\n  \"first_name\": \"Anna\",\n  \"last_name\": \"Smith\",\n  \"email\": \"annasmith23@gmail.com\"\n}\n```\n\nWith the `curl` command, we get the user with **Id 3**.\n\n## JSON Server GET request\n\nIn the next example we retrieve data with a **GET request**.\n\n`get_request.js`\n\n```js\nconst axios = require('axios');\n\naxios.get('http://localhost:3000/users')\n    .then(resp =\u003e {\n        data = resp.data;\n        data.forEach(e =\u003e {\n            console.log(`${e.first_name}, ${e.last_name}, ${e.email}`);\n        });\n    })\n    .catch(error =\u003e {\n        console.log(error);\n    });   \n```\n \nWith the axios module, we get all users as a JSON array and loop through it with `forEach()`.\n\n`$ node get_request.js `\n\n```markdown\nRobert, Schwartz, rob23@gmail.com\nLucy, Ballmer, lucyb56@gmail.com\nAnna, Smith, annasmith23@gmail.com\nRobert, Brown, bobbrown432@yahoo.com\nRoger, Bacon, rogerbacon12@yahoo.com\n```\n\nThis is the output of the example. We get **all users** and print their **full names** and **emails**.\n\n## JSON Server POST request\n\nWith a **POST request**, we create a new user.\n\n`post_request.js`\n\n```js\nconst axios = require('axios');\n\naxios.post('http://localhost:3000/users', {\n    id: 6,\n    first_name: 'Fred',\n    last_name: 'Blair',\n    email: 'freddyb34@gmail.com'\n}).then(resp =\u003e {\n    console.log(resp.data);\n}).catch(error =\u003e {\n    console.log(error);\n});   \n```\n\nA new user is created with **axios**.\n\n`$ node post_request.js `\n\n```json\n{ id: 6,\n  first_name: 'Fred',\n  last_name: 'Blair',\n  email: 'freddyb34@gmail.com' }\n```\n  \nThe server responds with a **newly created object**.\n\n`$ curl localhost:3000/users/6/`\n\n```json\n{\n  \"id\": 6,\n  \"first_name\": \"Fred\",\n  \"last_name\": \"Blair\",\n  \"email\": \"freddyb34@gmail.com\"\n}\n```\n\nWe verify the **newly created user** with the `curl` command.\n\n## JSON Server modify data with PUT request\n\nIn the following example we modify data with a **PUT request**.\n\n`put_request.js`\n\n```js\nconst axios = require('axios');\n\naxios.put('http://localhost:3000/users/6/', {\n    first_name: 'Fred',\n    last_name: 'Blair',\n    email: 'freddyb34@yahoo.com'\n}).then(resp =\u003e {\n\n    console.log(resp.data);\n}).catch(error =\u003e {\n\n    console.log(error);\n});\n```\n\nIn the example, we **modify** the **user's email address**.\n\n`$ node put_request.js `\n\n```json\n{ first_name: 'Fred',\n  last_name: 'Blair',\n  email: 'freddyb34@yahoo.com',\n  id: 6 }\n```\n\nThis is the output.\n\n## JSON Server DELETE request\n\nIn the following example, we show how to delete a user with a **DELETE request**.\n\n`delete_request.js`\n\n```js\nconst axios = require('axios');\n\naxios.delete('http://localhost:3000/users/1/')\n    .then(resp =\u003e {\n        console.log(resp.data)\n    }).catch(error =\u003e {\n        console.log(error);\n    });\n```\n\nIn the example, we **delete** the user with **Id 1**.\n\n`$ node delete_request.js `\n\n```json\n{}\n```\n\nThe server responds with empty JSON data.\n\n## JSON Server sorting data\n\nIn the next example, we **sort** our **data**.\n\n`sort_data.js`\n\n```js\nconst axios = require('axios');\n\naxios.get('http://localhost:3000/users?_sort=last_name\u0026_order=asc')\n    .then(resp =\u003e {\n        data = resp.data;\n        data.forEach(e =\u003e {\n            console.log(`${e.first_name}, ${e.last_name}, ${e.email}`)\n        });\n    }).catch(error =\u003e {\n        console.log(error);\n    });    \n```\n\nThe code example **sorts** data by the **users' last name in ascending order**. We use the `_sort` and `_order` **query parameters**.\n\n`$ node sort_data.js `\n\n```markdown\nRoger, Bacon, rogerbacon12@yahoo.com\nLucy, Ballmer, lucyb56@gmail.com\nFred, Blair, freddyb34@yahoo.com\nRobert, Brown, bobbrown432@yahoo.com\nRobert, Schwartz, rob23@gmail.com\nAnna, Smith, annasmith23@gmail.com\n```\n\nThis is the output.\n\n## JSON Server operators\n\nWe can use `_gte `and `_lte` for getting a **specific range of data**.\n\n`operators.js`\n\n```js\nconst axios = require('axios');\n\naxios.get('http://localhost:3000/users?id_gte=4')\n    .then(resp =\u003e {\n        console.log(resp.data)\n    }).catch(error =\u003e {\n        console.log(error);\n    }); \n```\n\nThe code example show users with **id greater than or equal to 4**.\n\n`$ node operators.js `\n\n```json\n[ { id: 4,\n    first_name: 'Robert',\n    last_name: 'Brown',\n    email: 'bobbrown432@yahoo.com' },\n  { id: '5',\n    first_name: 'Roger',\n    last_name: 'Bacon',\n    email: 'rogerbacon12@yahoo.com' },\n  { first_name: 'Fred',\n    last_name: 'Blair',\n    email: 'freddyb34@yahoo.com',\n    id: 6 } ]\n```\n\nThis is the output.\n\n## JSON Server full text search\n\nA **full text search** can be performed with the `q` **parameter**.\n\n`full_text_search.js`\n\n```js\nconst axios = require('axios');\n\naxios.get('http://localhost:3000/users?q=yahoo')\n    .then(resp =\u003e {\n        console.log(resp.data)\n    }).catch(error =\u003e {\n        console.log(error);\n    });\n```\n\nThe code example searches for the **yahoo term**.\n\n\n`$ node full_text_search.js `\n\n```json\n[ { id: 4,\n    first_name: 'Robert',\n    last_name: 'Brown',\n    email: 'bobbrown432@yahoo.com' },\n  { id: '5',\n    first_name: 'Roger',\n    last_name: 'Bacon',\n    email: 'rogerbacon12@yahoo.com' },\n  { first_name: 'Fred',\n    last_name: 'Blair',\n    email: 'freddyb34@yahoo.com',\n    id: 6 } ]\n```\n\nThe **search query** returned these **three users**.\n\n\nIn this tutorial, we have introduced the **JSON Server JavaScript library**.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedvlprs%2Fjson_server_lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthedvlprs%2Fjson_server_lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedvlprs%2Fjson_server_lib/lists"}