{"_id":"@abstractball/json-server","_rev":"1-1b67f9ac8635f51e4385abeee303c68d","name":"@abstractball/json-server","dist-tags":{"latest":"0.16.2"},"versions":{"0.16.2":{"name":"@abstractball/json-server","version":"0.16.2","description":"Get a full fake REST API with zero coding in less than 30 seconds","main":"./lib/server/index.js","bin":{"json-server":"lib/cli/bin.js"},"scripts":{"test":"npm run build && cross-env NODE_ENV=test jest && npm run lint","start":"babel-node -- src/cli/bin db.json -r routes.json","lint":"eslint . --ignore-path .gitignore","fix":"npm run lint -- --fix","build":"babel src -d lib","toc":"markdown-toc -i README.md","postversion":"git push && git push --tags","prepublishOnly":"npm test && npm run build && pkg-ok"},"dependencies":{"body-parser":"^1.19.0","chalk":"^3.0.0","compression":"^1.7.4","connect-pause":"^0.1.1","cors":"^2.8.5","errorhandler":"^1.5.1","express":"^4.17.1","express-urlrewrite":"^1.2.0","json-parse-helpfulerror":"^1.0.3","lodash":"^4.17.15","lodash-id":"^0.14.0","lowdb":"^1.0.0","method-override":"^3.0.0","morgan":"^1.9.1","nanoid":"^2.1.11","please-upgrade-node":"^3.2.0","pluralize":"^8.0.0","server-destroy":"^1.0.1","update-notifier":"^4.0.0","yargs":"^15.1.0"},"devDependencies":{"@babel/cli":"^7.8.4","@babel/core":"^7.8.4","@babel/node":"^7.8.4","@babel/preset-env":"^7.8.4","cross-env":"^7.0.0","eslint":"^6.8.0","eslint-config-prettier":"^6.10.0","eslint-config-standard":"^14.1.0","eslint-plugin-import":"^2.20.1","eslint-plugin-node":"^11.0.0","eslint-plugin-prettier":"^3.1.2","eslint-plugin-promise":"^4.2.1","eslint-plugin-standard":"^4.0.1","husky":"^4.2.1","jest":"^25.1.0","markdown-toc":"^1.2.0","mkdirp":"^1.0.3","npm-run-all":"^4.1.5","os-tmpdir":"^2.0.0","pkg-ok":"^2.3.1","prettier":"^1.19.1","rimraf":"^3.0.2","server-ready":"^0.3.1","supertest":"^4.0.2","temp-write":"^4.0.0"},"repository":{"type":"git","url":"git://github.com/typicode/json-server.git"},"keywords":["JSON","server","fake","REST","API","prototyping","mock","mocking","test","testing","rest","data","dummy","sandbox"],"author":{"name":"Typicode","email":"typicode@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/typicode/json-server/issues"},"homepage":"https://github.com/typicode/json-server","engines":{"node":">=10"},"husky":{"hooks":{"pre-commit":"npm test"}},"jest":{"testURL":"http://localhost/"},"gitHead":"9bd1e6f448883c9a105f9eea75fd120946ee25e6","_id":"@abstractball/json-server@0.16.2","_nodeVersion":"12.18.2","_npmVersion":"6.14.5","dist":{"integrity":"sha512-IvZ9AZS90PXTgs/YT5gnXF0jdHzchDoL/7BrFfk/KdPNcvcmaWjkC2USFY14wFT34WjydSiCeDSwMvfqR0ESuw==","shasum":"d36461a6b20e268d192cc02acb74d431aaa34dfb","tarball":"https://registry.npmjs.org/@abstractball/json-server/-/json-server-0.16.2.tgz","fileCount":39,"unpackedSize":67392,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfjr0iCRA9TVsSAnZWagAAIg4P/RV3vAzK03qXrLIH5vfm\nAIbEGl/Gbp51II/fb56dCh577O20PavGEO+HeLQjrcsGkaaUVZCqVyLVSOm7\nMf0UevS104hjIHxTqLOzVhedOB4CZfqvXwGaWtyrR6SzNMP2Xb2ZHSI51BwD\nZIAn3/SA+88KsfYbDO8sg3u9HYJbhw448tp9nh6P6Dxx1bFuGT5lRqp4/jlH\nu8KBbImEljkBe8zuhdZpY7yUxyCpXDHnmCPYC3bqVyMNvbVDnF6NSxt/E59f\n2WBStFH4Hzv3puotvjitjDGfg252VyncS2eswFXfnSGMXKA0Wighnth/5fAF\nhh1mqCyYzJg48xVHzGRmAeE4knxXhUd9WNunfhuYPBVIYqiK6q+qwggVEEYc\nfe1cpkHzgyMrWh1YbhpXl4jRObbxsYkEqq7Nc5INUPCCr2bkYPqGAhPyJcfw\nk0iJM+gLF+pxuPHmOwd6+qFshjJc+Co5E3hdjoOuGAUFl9Kiq6GrheEDFuQ3\nTIZ9ePj7Qqm2UKItHa2EHAN++Co3HF1+JZ5C4+h6TFAE1VZO4EKYLCcSbMle\nZEuVDpl+7RC2gVNHw9S4mrScTK/HWUfhoADoguXnJlccLHi5mKcu+6Eq3Frr\n9pRYSRV4HzlgQKxNpYqq02mmNECzTGY34ch91wfpz+snuOalzLQSyeemTGaJ\ndNLp\r\n=B8mr\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCoAjizzQzQ69FKRJbcwisPmKN+wnS2OOeX4+KRUcgFgQIgW43dkW1Cm/FRI1SSa9lZz2/qhKAD4pOVndglgM3cgok="}]},"maintainers":[{"name":"abstractball","email":"abstractball@protonmail.com"}],"_npmUser":{"name":"abstractball","email":"abstractball@protonmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/json-server_0.16.2_1603190050030_0.29126703340569127"},"_hasShrinkwrap":false}},"time":{"created":"2020-10-20T10:34:09.977Z","0.16.2":"2020-10-20T10:34:10.168Z","modified":"2022-04-04T11:02:29.209Z"},"maintainers":[{"name":"abstractball","email":"abstractball@protonmail.com"}],"description":"Get a full fake REST API with zero coding in less than 30 seconds","homepage":"https://github.com/typicode/json-server","keywords":["JSON","server","fake","REST","API","prototyping","mock","mocking","test","testing","rest","data","dummy","sandbox"],"repository":{"type":"git","url":"git://github.com/typicode/json-server.git"},"author":{"name":"Typicode","email":"typicode@gmail.com"},"bugs":{"url":"https://github.com/typicode/json-server/issues"},"license":"MIT","readme":"# JSON Server [![](https://travis-ci.org/typicode/json-server.svg?branch=master)](https://travis-ci.org/typicode/json-server) [![](https://badge.fury.io/js/json-server.svg)](http://badge.fury.io/js/json-server)\n\nGet a full fake REST API with __zero coding__ in __less than 30 seconds__ (seriously)\n\nCreated with <3 for front-end developers who need a quick back-end for prototyping and mocking.\n\n* [Egghead.io free video tutorial - Creating demo APIs with json-server](https://egghead.io/lessons/nodejs-creating-demo-apis-with-json-server)\n* [JSONPlaceholder - Live running version](https://jsonplaceholder.typicode.com)\n* [__My JSON Server__ - no installation required, use your own data](https://my-json-server.typicode.com)\n\nSee also:\n* :dog: [husky - Git hooks made easy](https://github.com/typicode/husky)\n* :hotel: [hotel - developer tool with local .localhost domain and https out of the box](https://github.com/typicode/hotel)\n\n<p>&nbsp;</p>\n\n<h2 align=\"center\">Gold sponsors 🥇</h2>\n\n<p>&nbsp;</p>\n\n<p align=\"center\">\n  <a href=\"https://tryretool.com/?utm_source=sponsor&utm_campaign=typicode\" target=\"_blank\">\n    <img src=\"https://i.imgur.com/IBItATn.png\" height=\"70px\">\n  </a>\n</p>\n\n<p>&nbsp;</p>\n\n<h2 align=\"center\">Bronze sponsors 🥉</h2>\n\n<p>&nbsp;</p>\n\n<p align=\"center\">\n  <a href=\"https://www.zinggrid.com/hello/json-server?utm_source=jsonserver&utm_medium=github&utm_campaign=sponsorship\" target=\"_blank\">\n    <img src=\"https://i.imgur.com/3mJGTAQ.png\" height=\"30px\">\n  </a>\n</p>\n\n<p>&nbsp;</p>\n\n[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)\n\n## Table of contents\n\n<!-- toc -->\n\n- [Getting started](#getting-started)\n- [Routes](#routes)\n  * [Plural routes](#plural-routes)\n  * [Singular routes](#singular-routes)\n  * [Filter](#filter)\n  * [Paginate](#paginate)\n  * [Sort](#sort)\n  * [Slice](#slice)\n  * [Operators](#operators)\n  * [Full-text search](#full-text-search)\n  * [Relationships](#relationships)\n  * [Database](#database)\n  * [Homepage](#homepage)\n- [Extras](#extras)\n  * [Static file server](#static-file-server)\n  * [Alternative port](#alternative-port)\n  * [Access from anywhere](#access-from-anywhere)\n  * [Remote schema](#remote-schema)\n  * [Generate random data](#generate-random-data)\n  * [HTTPS](#https)\n  * [Add custom routes](#add-custom-routes)\n  * [Add middlewares](#add-middlewares)\n  * [CLI usage](#cli-usage)\n  * [Module](#module)\n    + [Simple example](#simple-example)\n    + [Custom routes example](#custom-routes-example)\n    + [Access control example](#access-control-example)\n    + [Custom output example](#custom-output-example)\n    + [Rewriter example](#rewriter-example)\n    + [Mounting JSON Server on another endpoint example](#mounting-json-server-on-another-endpoint-example)\n    + [API](#api)\n  * [Deployment](#deployment)\n- [Links](#links)\n  * [Video](#video)\n  * [Articles](#articles)\n  * [Third-party tools](#third-party-tools)\n- [License](#license)\n\n<!-- tocstop -->\n\n## Getting started\n\nInstall JSON Server \n\n```\nnpm install -g json-server\n```\n\nCreate a `db.json` file with some data\n\n```json\n{\n  \"posts\": [\n    { \"id\": 1, \"title\": \"json-server\", \"author\": \"typicode\" }\n  ],\n  \"comments\": [\n    { \"id\": 1, \"body\": \"some comment\", \"postId\": 1 }\n  ],\n  \"profile\": { \"name\": \"typicode\" }\n}\n```\n\nStart JSON Server\n\n```bash\njson-server --watch db.json\n```\n\nNow if you go to [http://localhost:3000/posts/1](http://localhost:3000/posts/1), you'll get\n\n```json\n{ \"id\": 1, \"title\": \"json-server\", \"author\": \"typicode\" }\n```\n\nAlso when doing requests, it's good to know that:\n\n- If you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to `db.json` using [lowdb](https://github.com/typicode/lowdb).\n- Your request body JSON should be object enclosed, just like the GET output. (for example `{\"name\": \"Foobar\"}`)\n- Id values are not mutable. Any `id` value in the body of your PUT or PATCH request will be ignored. Only a value set in a POST request will be respected, but only if not already taken.\n- A POST, PUT or PATCH request should include a `Content-Type: application/json` header to use the JSON in the request body. Otherwise it will return a 2XX status code, but without changes being made to the data. \n\n## Routes\n\nBased on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-custom-routes) using `--routes`.\n\n### Plural routes\n\n```\nGET    /posts\nGET    /posts/1\nPOST   /posts\nPUT    /posts/1\nPATCH  /posts/1\nDELETE /posts/1\n```\n\n### Singular routes\n\n```\nGET    /profile\nPOST   /profile\nPUT    /profile\nPATCH  /profile\n```\n\n### Filter\n\nUse `.` to access deep properties\n\n```\nGET /posts?title=json-server&author=typicode\nGET /posts?id=1&id=2\nGET /comments?author.name=typicode\n```\n\n### Paginate\n\nUse `_page` and optionally `_limit` to paginate returned data.\n\nIn the `Link` header you'll get `first`, `prev`, `next` and `last` links.\n\n\n```\nGET /posts?_page=7\nGET /posts?_page=7&_limit=20\n```\n\n_10 items are returned by default_\n\n### Sort\n\nAdd `_sort` and `_order` (ascending order by default)\n\n```\nGET /posts?_sort=views&_order=asc\nGET /posts/1/comments?_sort=votes&_order=asc\n```\n\nFor multiple fields, use the following format:\n\n```\nGET /posts?_sort=user,views&_order=desc,asc\n```\n\n### Slice\n\nAdd `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response)\n\n```\nGET /posts?_start=20&_end=30\nGET /posts/1/comments?_start=20&_end=30\nGET /posts/1/comments?_start=20&_limit=10\n```\n\n_Works exactly as [Array.slice](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) (i.e. `_start` is inclusive and `_end` exclusive)_\n\n### Operators\n\nAdd `_gte` or `_lte` for getting a range\n\n```\nGET /posts?views_gte=10&views_lte=20\n```\n\nAdd `_ne` to exclude a value\n\n```\nGET /posts?id_ne=1\n```\n\nAdd `_like` to filter (RegExp supported)\n\n```\nGET /posts?title_like=server\n```\n\n### Full-text search\n\nAdd `q`\n\n```\nGET /posts?q=internet\n```\n\n### Relationships\n\nTo include children resources, add `_embed`\n\n```\nGET /posts?_embed=comments\nGET /posts/1?_embed=comments\n```\n\nTo include parent resource, add `_expand`\n\n```\nGET /comments?_expand=post\nGET /comments/1?_expand=post\n```\n\nTo get or create nested resources (by default one level, [add custom routes](#add-custom-routes) for more)\n\n```\nGET  /posts/1/comments\nPOST /posts/1/comments\n```\n\n### Database\n\n```\nGET /db\n```\n\n### Homepage\n\nReturns default index file or serves `./public` directory\n\n```\nGET /\n```\n\n## Extras\n\n### Static file server\n\nYou can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory\nor use `--static` to set a different static files directory.\n\n```bash\nmkdir public\necho 'hello world' > public/index.html\njson-server db.json\n```\n\n```bash\njson-server db.json --static ./some-other-dir\n```\n\n### Alternative port\n\nYou can start JSON Server on other ports with the `--port` flag:\n\n```bash\n$ json-server --watch db.json --port 3004\n```\n\n### Access from anywhere\n\nYou can access your fake API from anywhere using CORS and JSONP.\n\n### Remote schema\n\nYou can load remote schemas.\n\n```bash\n$ json-server http://example.com/file.json\n$ json-server http://jsonplaceholder.typicode.com/db\n```\n\n### Generate random data\n\nUsing JS instead of a JSON file, you can create data programmatically.\n\n```javascript\n// index.js\nmodule.exports = () => {\n  const data = { users: [] }\n  // Create 1000 users\n  for (let i = 0; i < 1000; i++) {\n    data.users.push({ id: i, name: `user${i}` })\n  }\n  return data\n}\n```\n\n```bash\n$ json-server index.js\n```\n\n__Tip__ use modules like [Faker](https://github.com/Marak/faker.js), [Casual](https://github.com/boo1ean/casual), [Chance](https://github.com/victorquinn/chancejs) or [JSON Schema Faker](https://github.com/json-schema-faker/json-schema-faker).\n\n### HTTPS\n\nThere are many ways to set up SSL in development. One simple way is to use [hotel](https://github.com/typicode/hotel).\n\n### Add custom routes\n\nCreate a `routes.json` file. Pay attention to start every route with `/`.\n\n```json\n{\n  \"/api/*\": \"/$1\",\n  \"/:resource/:id/show\": \"/:resource/:id\",\n  \"/posts/:category\": \"/posts?category=:category\",\n  \"/articles\\\\?id=:id\": \"/posts/:id\"\n}\n```\n\nStart JSON Server with `--routes` option.\n\n```bash\njson-server db.json --routes routes.json\n```\n\nNow you can access resources using additional routes.\n\n```sh\n/api/posts # → /posts\n/api/posts/1  # → /posts/1\n/posts/1/show # → /posts/1\n/posts/javascript # → /posts?category=javascript\n/articles?id=1 # → /posts/1\n```\n\n### Add middlewares\n\nYou can add your middlewares from the CLI using `--middlewares` option:\n\n```js\n// hello.js\nmodule.exports = (req, res, next) => {\n  res.header('X-Hello', 'World')\n  next()\n}\n```\n\n```bash\njson-server db.json --middlewares ./hello.js\njson-server db.json --middlewares ./first.js ./second.js\n```\n\n### CLI usage\n\n```\njson-server [options] <source>\n\nOptions:\n  --config, -c       Path to config file           [default: \"json-server.json\"]\n  --port, -p         Set port                                    [default: 3000]\n  --host, -H         Set host                             [default: \"localhost\"]\n  --watch, -w        Watch file(s)                                     [boolean]\n  --routes, -r       Path to routes file\n  --middlewares, -m  Paths to middleware files                           [array]\n  --static, -s       Set static files directory\n  --read-only, --ro  Allow only GET requests                           [boolean]\n  --no-cors, --nc    Disable Cross-Origin Resource Sharing             [boolean]\n  --no-gzip, --ng    Disable GZIP Content-Encoding                     [boolean]\n  --snapshots, -S    Set snapshots directory                      [default: \".\"]\n  --delay, -d        Add delay to responses (ms)\n  --id, -i           Set database id property (e.g. _id)         [default: \"id\"]\n  --foreignKeySuffix, --fks  Set foreign key suffix, (e.g. _id as in post_id)\n                                                                 [default: \"Id\"]\n  --quiet, -q        Suppress log messages from output                 [boolean]\n  --help, -h         Show help                                         [boolean]\n  --version, -v      Show version number                               [boolean]\n\nExamples:\n  json-server db.json\n  json-server file.js\n  json-server http://example.com/db.json\n\nhttps://github.com/typicode/json-server\n```\n\nYou can also set options in a `json-server.json` configuration file.\n\n```json\n{\n  \"port\": 3000\n}\n```\n\n### Module\n\nIf you need to add authentication, validation, or __any behavior__, you can use the project as a module in combination with other Express middlewares.\n\n#### Simple example\n\n```sh\n$ npm install json-server --save-dev\n```\n\n```js\n// server.js\nconst jsonServer = require('json-server')\nconst server = jsonServer.create()\nconst router = jsonServer.router('db.json')\nconst middlewares = jsonServer.defaults()\n\nserver.use(middlewares)\nserver.use(router)\nserver.listen(3000, () => {\n  console.log('JSON Server is running')\n})\n```\n\n```sh\n$ node server.js\n```\n\nThe path you provide to the `jsonServer.router` function  is relative to the directory from where you launch your node process. If you run the above code from another directory, it’s better to use an absolute path:\n\n```js\nconst path = require('path')\nconst router = jsonServer.router(path.join(__dirname, 'db.json'))\n```\n\nFor an in-memory database, simply pass an object to `jsonServer.router()`.\n\nPlease note also that `jsonServer.router()` can be used in existing Express projects.\n\n#### Custom routes example\n\nLet's say you want a route that echoes query parameters and another one that set a timestamp on every resource created.\n\n```js\nconst jsonServer = require('json-server')\nconst server = jsonServer.create()\nconst router = jsonServer.router('db.json')\nconst middlewares = jsonServer.defaults()\n\n// Set default middlewares (logger, static, cors and no-cache)\nserver.use(middlewares)\n\n// Add custom routes before JSON Server router\nserver.get('/echo', (req, res) => {\n  res.jsonp(req.query)\n})\n\n// To handle POST, PUT and PATCH you need to use a body-parser\n// You can use the one used by JSON Server\nserver.use(jsonServer.bodyParser)\nserver.use((req, res, next) => {\n  if (req.method === 'POST') {\n    req.body.createdAt = Date.now()\n  }\n  // Continue to JSON Server router\n  next()\n})\n\n// Use default router\nserver.use(router)\nserver.listen(3000, () => {\n  console.log('JSON Server is running')\n})\n```\n\n#### Access control example\n\n```js\nconst jsonServer = require('json-server')\nconst server = jsonServer.create()\nconst router = jsonServer.router('db.json')\nconst middlewares = jsonServer.defaults()\n\nserver.use(middlewares)\nserver.use((req, res, next) => {\n if (isAuthorized(req)) { // add your authorization logic here\n   next() // continue to JSON Server router\n } else {\n   res.sendStatus(401)\n }\n})\nserver.use(router)\nserver.listen(3000, () => {\n  console.log('JSON Server is running')\n})\n```\n#### Custom output example\n\nTo modify responses, overwrite `router.render` method:\n\n```javascript\n// In this example, returned resources will be wrapped in a body property\nrouter.render = (req, res) => {\n  res.jsonp({\n    body: res.locals.data\n  })\n}\n```\n\nYou can set your own status code for the response:\n\n\n```javascript\n// In this example we simulate a server side error response\nrouter.render = (req, res) => {\n  res.status(500).jsonp({\n    error: \"error message here\"\n  })\n}\n```\n\n#### Rewriter example\n\nTo add rewrite rules, use `jsonServer.rewriter()`:\n\n```javascript\n// Add this before server.use(router)\nserver.use(jsonServer.rewriter({\n  '/api/*': '/$1',\n  '/blog/:resource/:id/show': '/:resource/:id'\n}))\n```\n\n#### Mounting JSON Server on another endpoint example\n\nAlternatively, you can also mount the router on `/api`.\n\n```javascript\nserver.use('/api', router)\n```\n\n#### API\n\n__`jsonServer.create()`__\n\nReturns an Express server.\n\n__`jsonServer.defaults([options])`__\n\nReturns middlewares used by JSON Server.\n\n* options\n  * `static` path to static files\n  * `logger` enable logger middleware (default: true)\n  * `bodyParser` enable body-parser middleware (default: true)\n  * `noCors` disable CORS (default: false)\n  * `readOnly` accept only GET requests (default: false)\n\n__`jsonServer.router([path|object])`__\n\nReturns JSON Server router.\n\n### Deployment\n\nYou can deploy JSON Server. For example, [JSONPlaceholder](http://jsonplaceholder.typicode.com) is an online fake API powered by JSON Server and running on Heroku.\n\n## Links\n\n### Video\n\n* [Creating Demo APIs with json-server on egghead.io](https://egghead.io/lessons/nodejs-creating-demo-apis-with-json-server)\n\n### Articles\n\n* [Node Module Of The Week - json-server](http://nmotw.in/json-server/)\n* [ng-admin: Add an AngularJS admin GUI to any RESTful API](http://marmelab.com/blog/2014/09/15/easy-backend-for-your-restful-api.html)\n* [Fast prototyping using Restangular and Json-server](https://glebbahmutov.com/blog/fast-prototyping-restangular-and-json-server/)\n* [Create a Mock REST API in Seconds for Prototyping your Frontend](https://coligo.io/create-mock-rest-api-with-json-server/)\n* [No API? No Problem! Rapid Development via Mock APIs](https://medium.com/@housecor/rapid-development-via-mock-apis-e559087be066#.93d7w8oro)\n* [Zero Code REST With json-server](https://dzone.com/articles/zero-code-rest-with-json-server)\n\n### Third-party tools\n\n* [Grunt JSON Server](https://github.com/tfiwm/grunt-json-server)\n* [Docker JSON Server](https://github.com/clue/docker-json-server)\n* [JSON Server GUI](https://github.com/naholyr/json-server-gui)\n* [JSON file generator](https://github.com/dfsq/json-server-init)\n* [JSON Server extension](https://github.com/maty21/json-server-extension)\n\n## License\n\nMIT\n\n[Supporters](https://thanks.typicode.com) ✨\n","readmeFilename":"README.md"}