Skip to content

Commit a7d427f

Browse files
committed
Update README.md
1 parent 09aee3b commit a7d427f

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

README.md

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ Create a `db.json` file
1414
```javascript
1515
{
1616
"posts": [
17-
{ "id": 1, "title": "json-server", "author": "typicode" }
17+
{
18+
"id": 1,
19+
"title": "json-server",
20+
"author": "typicode"
21+
}
1822
],
1923
"comments": [
20-
{ "id": 1, "body": "some comment", "postId": 1 }
24+
{
25+
"id": 1,
26+
"body": "some comment",
27+
"postId": 1
28+
}
2129
]
2230
}
2331
```
@@ -31,31 +39,34 @@ $ json-server db.json
3139
Now if you go to [http://localhost:3000/posts/1](), you'll get
3240

3341
```javascript
34-
{ "id": 1, "title": "json-server", "author": "typicode" }
42+
{
43+
"id": 1,
44+
"title": "json-server",
45+
"author": "typicode"
46+
}
3547
```
3648

3749
Also, if you make POST, PUT, PATCH or DELETE requests, changes will be automatically saved to `db.json` using [lowdb](https://github.com/typicode/lowdb)
3850

3951
## Routes
4052

41-
Based on the previous `db.json`, here are all the available routes.
53+
Based on the previous `db.json` file, here are all the available routes. If you need more customization, you can use the project as a [module](https://github.com/typicode/json-server#module).
4254

4355
```
44-
GET /posts
45-
GET /posts/1
46-
GET /posts/1/comments
47-
GET /posts?title=json-server&author=typicode
48-
POST /posts
49-
PUT /posts/1
50-
PATCH /posts/1
51-
DEL /posts/1
56+
GET /posts
57+
GET /posts/1
58+
GET /posts/1/comments
59+
GET /posts?title=json-server&author=typicode
60+
POST /posts
61+
PUT /posts/1
62+
PATCH /posts/1
63+
DELETE /posts/1
5264
```
5365

54-
To slice resources, add `_start` and `_end` or `_limit`. An `X-Total-Count` header is included in the response.
66+
To slice resources, add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response).
5567

5668
```
5769
GET /posts?_start=20&_end=30
58-
GET /posts?_start=20&_limit=10
5970
GET /posts/1/comments?_start=20&_end=30
6071
```
6172

@@ -130,19 +141,26 @@ $ json-server index.js
130141

131142
### Module
132143

133-
You can use JSON Server as a module:
134-
135144
```javascript
136145
var jsonServer = require('json-server')
137146

138-
var server = jsonServer.create() // Express server
139-
server.use(jsonServer.defaults) // Default middlewares (logger, public, cors)
140-
server.use(jsonServer.router('db.json')) // Express router
147+
// Express server
148+
var server = jsonServer.create()
149+
150+
// Default middlewares (logger, public, cors)
151+
server.use(jsonServer.defaults)
152+
153+
// Add other Express middlewares if needed (authentication, redirections, ...)
154+
// ...
155+
156+
// Express router
157+
server.use(jsonServer.router('db.json'))
141158

142159
server.listen(3000)
143160
```
144161

145162
For an in-memory database, you can pass an object to `jsonServer.route()`.
163+
Please note also that you can use the `jsonServer.router()` in existing Express servers.
146164

147165
### Deployment
148166

0 commit comments

Comments
 (0)