|
| 1 | + |
| 2 | + |
| 3 | +__JSON Server__ |
| 4 | + |
| 5 | +> Give it a JSON or JS seed file and it will serve it through REST routes. |
| 6 | +
|
| 7 | +_Created with :heart: for front-end developers who need a flexible back-end for quick prototyping and mocking._ |
| 8 | + |
| 9 | + |
| 10 | +## Examples |
| 11 | + |
| 12 | +### Command line interface |
| 13 | + |
| 14 | +```bash |
| 15 | +$ cat db.json |
| 16 | +{ |
| 17 | + posts: [ |
| 18 | + { id: 1, body: 'foo' } |
| 19 | + ] |
| 20 | +} |
| 21 | +$ json-server --file db.json |
| 22 | +$ curl -i http://localhost:3000/posts/1 |
| 23 | +``` |
| 24 | +
|
| 25 | +### Node module |
| 26 | +
|
| 27 | +```javascript |
| 28 | +var server = require('json-server'); |
| 29 | + |
| 30 | +var db = { |
| 31 | + posts: [ |
| 32 | + {id: 1, body: 'foo'} |
| 33 | + ] |
| 34 | +} |
| 35 | + |
| 36 | +server.run(db); |
| 37 | +``` |
| 38 | +
|
| 39 | +
|
| 40 | +## Why? |
| 41 | +
|
| 42 | +* Supports __GET__ but also __POST__, __PUT__, __DELETE__ and even __PATCH__ requests |
| 43 | +* Can be used from anywhere through __cross domain__ requests (JSONP or CORS) |
| 44 | +* Can load remote JSON files |
| 45 | +* Can be deployed on Nodejitsu, Heroku, ... |
| 46 | +
|
| 47 | +
|
| 48 | +## Installation |
| 49 | +
|
| 50 | +```bash |
| 51 | +$ npm install -g json-server |
| 52 | +``` |
| 53 | +
|
| 54 | +
|
| 55 | +## Usage |
| 56 | +
|
| 57 | +### Command line interface |
| 58 | +
|
| 59 | +```bash |
| 60 | +json-server --help |
| 61 | + |
| 62 | + Usage: json-server [options] |
| 63 | + |
| 64 | + Options: |
| 65 | + |
| 66 | + -h, --help output usage information |
| 67 | + -V, --version output the version number |
| 68 | + -f --file <file> load db from a js or json file |
| 69 | + -u --url <url> load db from a URL |
| 70 | + -p --port [port] server port |
| 71 | + --read-only read only mode |
| 72 | +``` |
| 73 | +
|
| 74 | +JSON Server can load JSON from multiple sources: |
| 75 | +
|
| 76 | +```bash |
| 77 | +$ json-server --file db.json |
| 78 | +$ json-server --file seed.js |
| 79 | +$ json-server --url http://example.com/db.json |
| 80 | +``` |
| 81 | +
|
| 82 | +And be run in read-only mode (useful if deployed on a public server): |
| 83 | +
|
| 84 | +```bash |
| 85 | +$ json-server --file db.json --read-only |
| 86 | +``` |
| 87 | +
|
| 88 | +#### Input |
| 89 | +
|
| 90 | +Here's 2 examples showing how to format JSON or JS seed file: |
| 91 | +
|
| 92 | +* __db.json__ |
| 93 | +```javascript |
| 94 | +{ |
| 95 | + posts: [ |
| 96 | + { id: 1, body: 'foo'}, |
| 97 | + { id: 2, body: 'bar'} |
| 98 | + ], |
| 99 | + comments: [ |
| 100 | + { id: 1, body: 'baz', postId: 1} |
| 101 | + { id: 2, body: 'qux', postId: 2} |
| 102 | + ] |
| 103 | +} |
| 104 | +``` |
| 105 | +
|
| 106 | +* __seed.js__ |
| 107 | +```javascript |
| 108 | +exports.run = function() { |
| 109 | + var data = {}; |
| 110 | + |
| 111 | + data.posts = []; |
| 112 | + data.posts.push({id: 1, body: 'foo'}); |
| 113 | + //... |
| 114 | +
|
| 115 | + return data; |
| 116 | +} |
| 117 | +``` |
| 118 | +
|
| 119 | +JSON Server expects JS files to export a ```run``` method that returns an object. |
| 120 | +
|
| 121 | +Seed files are useful if you need to programmaticaly create a lot of data. |
| 122 | +
|
| 123 | +
|
| 124 | +### Node module |
| 125 | +
|
| 126 | +#### run(db, [options]) |
| 127 | +
|
| 128 | +```javascript |
| 129 | +var server = require('json-server'), |
| 130 | + db = require('./seed').run(); |
| 131 | +
|
| 132 | +var options = { port: 4000, readOnly: true }; |
| 133 | +
|
| 134 | +server.run(db, options); |
| 135 | +``` |
| 136 | +
|
| 137 | +By default, ```port``` is set to 3000 and ```readOnly``` to false. |
| 138 | +
|
| 139 | +## Routes |
| 140 | +
|
| 141 | +``` |
| 142 | +GET /:resource |
| 143 | +GET /:resource?attr=&attr=& |
| 144 | +GET /:parent/:parentId/:resource |
| 145 | +GET /:resource/:id |
| 146 | +POST /:resource |
| 147 | +PUT /:resource/:id |
| 148 | +PATCH /:resource/:id |
| 149 | +DEL /:resource/:id |
| 150 | +``` |
| 151 | +
|
| 152 | +For routes usage information, have a look at [JSONPlaceholder](https://github.com/typicode/jsonplaceholder) code examples. |
| 153 | +
|
| 154 | +``` |
| 155 | +GET /db |
| 156 | +``` |
| 157 | +
|
| 158 | +Returns database state. |
| 159 | +
|
| 160 | +
|
| 161 | +``` |
| 162 | +GET / |
| 163 | +``` |
| 164 | +
|
| 165 | +Returns default index file or content of ./public/index.html (useful if you need to set a custom home page). |
| 166 | +
|
| 167 | +
|
| 168 | +## Support |
| 169 | +
|
| 170 | +If you like the project, please tell your friends about it, star it or give feedback :) It's very much appreciated! |
| 171 | +
|
| 172 | +For project updates or to get in touch, [@typicode](http://twitter.com/typicde). You can also send me a mail. |
| 173 | +
|
| 174 | +## Test |
| 175 | +
|
| 176 | +```bash |
| 177 | +$ npm install |
| 178 | +$ npm test |
| 179 | +``` |
0 commit comments