4
4
5
5
# JSON Server [ ![ Build Status] ( https://travis-ci.org/typicode/json-server.svg )] ( https://travis-ci.org/typicode/json-server ) [ ![ NPM version] ( https://badge.fury.io/js/json-server.svg )] ( http://badge.fury.io/js/json-server )
6
6
7
- Give it a JSON or JS seed file and it will serve it through REST routes.
7
+ > Give it a JSON or JS file and it will serve it through REST routes.
8
8
9
9
Created with <3 for front-end developers who need a flexible back-end for quick prototyping and mocking.
10
10
11
11
_ Powers [ JSONPlaceholder] ( http://jsonplaceholder.typicode.com ) _
12
12
13
- ## Examples
13
+ ## Usage
14
14
15
- ### Command line interface
15
+ ### CLI
16
+
17
+ Create a ` db.json ` file:
16
18
17
19
``` javascript
18
- // db.json
19
20
{
20
21
" posts" : [
21
22
{ " id" : 1 , " body" : " foo" }
22
23
]
23
24
}
24
25
```
25
26
26
- ``` bash
27
- $ json-server db.json
28
- $ curl -i http://localhost:3000/posts/1
29
- ```
27
+ Then run ` json-server db.json ` and go to ` http://localhost:3000/posts/1 ` .
30
28
31
- ### Node module
29
+ You should get ` { "id": 1, "body": "foo" } ` .
30
+
31
+ ### Module
32
32
33
33
``` javascript
34
34
var server = require (' json-server' );
@@ -40,24 +40,23 @@ server({
40
40
}).listen (3000 );
41
41
```
42
42
43
- You can find a running demo here: http://jsonplaceholder.typicode.com .
43
+ __ Tip __ You can mount json-server in Express apps .
44
44
45
- ## Why?
45
+ ## Features
46
46
47
47
* Lets you use plain JSON or simple JS file
48
48
* Supports __ GET__ but also __ POST__ , __ PUT__ , __ DELETE__ and even __ PATCH__ requests
49
49
* Can be used from anywhere through __ cross domain__ requests (JSONP or CORS)
50
50
* Can load remote JSON files ([ JSON Generator] ( http://www.json-generator.com/ ) , ...)
51
51
* Can be deployed on Nodejitsu, Heroku, ...
52
52
53
-
54
- ## Installation
53
+ ## Install
55
54
56
55
``` bash
57
56
$ npm install -g json-server
58
57
```
59
58
60
- ## CLI usage
59
+ ## CLI options
61
60
62
61
``` bash
63
62
json-server < source>
@@ -78,7 +77,7 @@ Options:
78
77
79
78
Here's 2 examples showing how to format JSON or JS seed file:
80
79
81
- * __ db.json __
80
+ __ JSON __
82
81
83
82
``` javascript
84
83
{
@@ -93,7 +92,7 @@ Here's 2 examples showing how to format JSON or JS seed file:
93
92
}
94
93
```
95
94
96
- * __ file.js __
95
+ __ JS __
97
96
98
97
``` javascript
99
98
module .exports = function () {
@@ -109,39 +108,41 @@ module.exports = function() {
109
108
110
109
JSON Server expects JS files to export a function that returns an object.
111
110
112
- Seed files are useful if you need to programmaticaly create a lot of data.
111
+ JS files are useful if you need to programmaticaly create a lot of data.
112
+
113
+ ## Available routes
113
114
114
- ## Routes
115
+ Let's say we have ` posts ` , here's the routes you can use.
115
116
116
117
```
117
- GET /:resource
118
- GET /:resource?filter=&filter=&
119
- GET /:parent/:parentId/:resource
120
- GET /:resource/:id
121
- POST /:resource
122
- PUT /:resource/:id
123
- PATCH /:resource/:id
124
- DEL /:resource/:id
118
+ GET /posts
119
+ GET /posts?title=jsonserver&author=typicode
120
+ GET /posts/1/comments
121
+ GET /posts/1
122
+ POST /posts
123
+ PUT /posts/1
124
+ PATCH /posts/1
125
+ DEL /posts/1
125
126
```
126
127
127
128
To slice resources, add ` _start ` and ` _end ` .
128
129
129
130
```
130
- GET /:resource ?_start=&_end=
131
- GET /:parent/:parentId/:resource ?_start=&_end=
131
+ GET /posts ?_start=0 &_end=10
132
+ GET /posts/1/comments ?_start=0 &_end=10
132
133
```
133
134
134
135
To sort resources, add ` _sort ` and ` _order ` (ascending order by default).
135
136
136
137
```
137
- GET /:resource ?_sort=&_order=(ASC| DESC)
138
- GET /:parent/:parentId/:resource ?_sort=&_order=( ASC|DESC)
138
+ GET /posts ?_sort=views &_order=DESC
139
+ GET /posts/1/comments ?_sort=votes &_order=ASC
139
140
```
140
141
141
142
To make a full-text search on resources, add ` q ` .
142
143
143
144
```
144
- GET /:resource ?q=
145
+ GET /posts ?q=internet
145
146
```
146
147
147
148
Returns database.
@@ -150,13 +151,13 @@ Returns database.
150
151
GET /db
151
152
```
152
153
153
- Returns default index file or content of ./public/index.html (useful if you need to set a custom home page).
154
+ Returns default index file or content of ` ./public/index.html ` (useful if you need to set a custom home page).
154
155
155
156
```
156
157
GET /
157
158
```
158
159
159
- For routes usage examples, have a look at [ JSONPlaceholder] ( https://github.com/typicode/jsonplaceholder ) 's README.
160
+ For more routes usage examples, have a look at [ JSONPlaceholder] ( https://github.com/typicode/jsonplaceholder ) 's README.
160
161
161
162
## Links
162
163
0 commit comments