Skip to content

Commit 0bb11ae

Browse files
committed
Update README.md
1 parent 1bafe62 commit 0bb11ae

File tree

1 file changed

+84
-92
lines changed

1 file changed

+84
-92
lines changed

README.md

Lines changed: 84 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,48 @@
1-
<p align="center">
2-
<img height="56" width="64" src="http://i.imgur.com/QRlAg0b.png"/>
3-
</p>
4-
51
# 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)
62

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)
104

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.
126

13-
## Usage
7+
Powers [JSONPlaceholder](http://jsonplaceholder.typicode.com)
148

15-
### CLI
9+
## Example
1610

17-
Create a `db.json` file:
11+
Create a `db.json` file
1812

1913
```javascript
2014
{
2115
"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 }
2320
]
2421
}
2522
```
2623

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
5825

5926
```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
7228
```
7329

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:
7931

8032
```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"
10436
}
10537
```
10638

107-
JSON Server expects JS files to export a function that returns an object.
39+
## Routes
10840

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:
11442

11543
```
11644
GET /posts
117-
GET /posts?title=jsonserver&author=typicode
45+
GET /posts?title=json-server&author=typicode
11846
GET /posts/1/comments
11947
GET /posts/1
12048
POST /posts
@@ -149,13 +77,73 @@ Returns database.
14977
GET /db
15078
```
15179

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.
15381

15482
```
15583
GET /
15684
```
15785

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.
159147

160148
## Links
161149

@@ -169,3 +157,7 @@ For more routes usage examples, have a look at [JSONPlaceholder](https://github.
169157
* [Grunt JSON Server](https://github.com/tfiwm/grunt-json-server)
170158
* [docker-json-server](https://github.com/clue/docker-json-server)
171159
* [JSON Server GUI](https://github.com/naholyr/json-server-gui)
160+
161+
## License
162+
163+
MIT - [Typicode](https://github.com/typicode)

0 commit comments

Comments
 (0)