1
- <p align =" center " >
2
- <img height =" 56 " width =" 64 " src =" http://i.imgur.com/QRlAg0b.png " />
3
- </p >
4
-
5
1
# 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
2
7
- > Give it a JSON or JS file and it will serve it through REST routes.
8
-
9
- Created with <3 for front-end developers who need a flexible back-end for quick prototyping and mocking.
3
+ Get a full fake REST API with __ zero coding__ in __ less than 30 seconds__ (seriously)
10
4
11
- _ Powers [ JSONPlaceholder ] ( http://jsonplaceholder.typicode.com ) _
5
+ Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.
12
6
13
- ## Usage
7
+ Powers [ JSONPlaceholder ] ( http://jsonplaceholder.typicode.com )
14
8
15
- ### CLI
9
+ ## Example
16
10
17
- Create a ` db.json ` file:
11
+ Create a ` db.json ` file
18
12
19
13
``` javascript
20
14
{
21
15
" posts" : [
22
- { " id" : 1 , " body" : " foo" }
16
+ { " id" : 1 , " title" : " json-server" , " author" : " typicode" }
17
+ ],
18
+ " comments" : [
19
+ { " id" : 1 , " body" : " some comment" , " postId" : 1 }
23
20
]
24
21
}
25
22
```
26
23
27
- Then run ` json-server db.json ` and go to ` http://localhost:3000/posts/1 ` .
28
-
29
- You should get ` { "id": 1, "body": "foo" } ` .
30
-
31
- ### Module
32
-
33
- ``` javascript
34
- var server = require (' json-server' );
35
-
36
- server ({
37
- posts: [
38
- { id: 1 , body: ' foo' }
39
- ]
40
- }).listen (3000 );
41
- ```
42
-
43
- ## Features
44
-
45
- * Lets you use plain JSON or simple JS file
46
- * Supports __ GET__ but also __ POST__ , __ PUT__ , __ DELETE__ and even __ PATCH__ requests
47
- * Can be used from anywhere through __ cross domain__ requests (JSONP or CORS)
48
- * Can load remote JSON files ([ JSON Generator] ( http://www.json-generator.com/ ) , ...)
49
- * Can be deployed on Nodejitsu, Heroku, ...
50
-
51
- ## Install
52
-
53
- ``` bash
54
- $ npm install -g json-server
55
- ```
56
-
57
- ## CLI options
24
+ Start JSON Server
58
25
59
26
``` bash
60
- json-server < source>
61
-
62
- Examples:
63
- json-server db.json
64
- json-server file.js
65
- json-server http://example.com/db.json
66
-
67
-
68
- Options:
69
- --help, -h Show help
70
- --version, -v Show version number
71
- --port, -p Set port [default: 3000]
27
+ $ json-server db.json
72
28
```
73
29
74
- #### Input
75
-
76
- Here's 2 examples showing how to format JSON or JS seed file:
77
-
78
- __ JSON__
30
+ Now if you go to [ http://localhost:3000/posts/1 ] ( ) , you will get:
79
31
80
32
``` javascript
81
- {
82
- " posts" : [
83
- { " id" : 1 , " body" : " foo" },
84
- { " id" : 2 , " body" : " bar" }
85
- ],
86
- " comments" : [
87
- { " id" : 1 , " body" : " baz" , " postId" : 1 },
88
- { " id" : 2 , " body" : " qux" , " postId" : 2 }
89
- ]
90
- }
91
- ```
92
-
93
- __ JS__
94
-
95
- ``` javascript
96
- module .exports = function () {
97
- var data = {};
98
-
99
- data .posts = [];
100
- data .posts .push ({ id: 1 , body: ' foo' });
101
- // ...
102
-
103
- return data;
33
+ {
34
+ " id" : 1 ,
35
+ " body" : " foo"
104
36
}
105
37
```
106
38
107
- JSON Server expects JS files to export a function that returns an object.
39
+ ## Routes
108
40
109
- JS files are useful if you need to programmaticaly create a lot of data.
110
-
111
- ## Available routes
112
-
113
- Let's say we have ` posts ` , here's the routes you can use.
41
+ In fact, you instantly get all these routes:
114
42
115
43
```
116
44
GET /posts
117
- GET /posts?title=jsonserver &author=typicode
45
+ GET /posts?title=json-server &author=typicode
118
46
GET /posts/1/comments
119
47
GET /posts/1
120
48
POST /posts
@@ -149,13 +77,73 @@ Returns database.
149
77
GET /db
150
78
```
151
79
152
- Returns default index file or content of ` ./public/index.html ` (useful if you need to set a custom home page) .
80
+ Returns default index file or serves ` ./public ` directory .
153
81
154
82
```
155
83
GET /
156
84
```
157
85
158
- For more routes usage examples, have a look at [ JSONPlaceholder] ( https://github.com/typicode/jsonplaceholder ) 's README.
86
+ ## Install
87
+
88
+ ``` bash
89
+ $ npm install -g json-server
90
+ ```
91
+
92
+ ## Extras
93
+
94
+ ### Static file server
95
+
96
+ You can use JSON Server to serve your HTML, JS and CSS, simply create a ` ./public ` directory.
97
+
98
+ ### Access from anywhere
99
+
100
+ You can access your fake API from anywhere using CORS and JSONP.
101
+
102
+ ### Remote schema
103
+
104
+ You can load remote schemas:
105
+
106
+ ``` bash
107
+ $ json-server http://example.com/file.json
108
+ $ json-server http://jsonplaceholder.typicode.com/db
109
+ ```
110
+
111
+ ### JS file support
112
+
113
+ You can use JS to programmatically create data:
114
+
115
+ ``` javascript
116
+ module .exports = function () {
117
+ data = { users: [] }
118
+ // Create 1000 users
119
+ for (var i = 0 ; i < 1000 ; i++ ) {
120
+ data .users .push ({ name: ' user' + i })
121
+ }
122
+ return data
123
+ }
124
+ ```
125
+
126
+ ``` bash
127
+ $ json-server index.js
128
+ ```
129
+
130
+ ### Module
131
+
132
+ You can use JSON Server as a module:
133
+
134
+ ``` javascript
135
+ var server = require (' json-server' )
136
+
137
+ server ({
138
+ posts: [
139
+ { id: 1 , body: ' foo' }
140
+ ]
141
+ }).listen (3000 )
142
+ ```
143
+
144
+ ### Deployment
145
+
146
+ You can deploy JSON Server. For example, [ JSONPlaceholder] ( http://jsonplaceholder.typicode.com ) is an online fake API powered by JSON Server and running on Heroku.
159
147
160
148
## Links
161
149
@@ -169,3 +157,7 @@ For more routes usage examples, have a look at [JSONPlaceholder](https://github.
169
157
* [ Grunt JSON Server] ( https://github.com/tfiwm/grunt-json-server )
170
158
* [ docker-json-server] ( https://github.com/clue/docker-json-server )
171
159
* [ JSON Server GUI] ( https://github.com/naholyr/json-server-gui )
160
+
161
+ ## License
162
+
163
+ MIT - [ Typicode] ( https://github.com/typicode )
0 commit comments