Skip to content

Commit 682f921

Browse files
committed
Update README.md
1 parent e82fa0c commit 682f921

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

README.md

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,31 +292,62 @@ You can also set options in a `json-server.json` configuration file.
292292

293293
### Module
294294

295-
If you need to add authentication, validation, you can use the project as a module in combination with other Express middlewares.
295+
If you need to add authentication, validation, or __any behavior__, you can use the project as a module in combination with other Express middlewares.
296296

297-
```javascript
297+
__Simple example__
298+
299+
```js
298300
var jsonServer = require('json-server')
301+
var server = jsonServer.create()
302+
var router = jsonServer.router('db.json')
303+
var middlewares = jsonServer.defaults()
304+
305+
server.use(middlewares)
306+
server.use(router)
307+
server.listen(3000, function () {
308+
console.log('JSON Server is running')
309+
})
310+
```
311+
312+
For an in-memory database, you can pass an object to `jsonServer.router()`.
313+
Please note also that `jsonServer.router()` can be used in existing Express projects.
314+
315+
__Custom routes example__
299316

300-
// Returns an Express server
317+
Let's say you want a route that echoes query parameters and another one that set a timestamp on every resource created.
318+
319+
```js
320+
var jsonServer = require('json-server')
301321
var server = jsonServer.create()
322+
var router = jsonServer.router('db.json')
323+
var middlewares = jsonServer.defaults()
302324

303325
// Set default middlewares (logger, static, cors and no-cache)
304-
server.use(jsonServer.defaults())
326+
server.use(middlewares)
305327

306-
// Add custom routes
307-
// server.get('/custom', function (req, res) { res.json({ msg: 'hello' }) })
328+
// Add custom routes before JSON Server router
329+
server.get('/echo', function (req, res) {
330+
res.jsonp(req.query)
331+
})
308332

309-
// Returns an Express router
310-
var router = jsonServer.router('db.json')
311-
server.use(router)
333+
server.use(function (req, res, next) {
334+
if (req.method === 'POST') {
335+
req.body.createdAt = Date.now()
336+
}
337+
// Continue to JSON Server router
338+
next()
339+
})
312340

313-
server.listen(3000)
341+
// Use default router
342+
server.use(router)
343+
server.listen(3000, function () {
344+
console.log('JSON Server is running')
345+
})
314346
```
315347

316-
For an in-memory database, you can pass an object to `jsonServer.router()`.
317-
Please note also that `jsonServer.router()` can be used in existing Express projects.
348+
__Custom output example__
318349

319-
To modify responses, use `router.render()`:
350+
To modify responses, overwrite `router.render` method:
320351

321352
```javascript
322353
// In this example, returned resources will be wrapped in a body property
@@ -327,6 +358,8 @@ router.render = function (req, res) {
327358
}
328359
```
329360

361+
__Rewriter example__
362+
330363
To add rewrite rules, use `jsonServer.rewriter()`:
331364

332365
```javascript
@@ -337,7 +370,9 @@ server.use(jsonServer.rewriter({
337370
}))
338371
```
339372

340-
Alternatively, you can also mount the router on another path.
373+
__Mounting JSON Server on another endpoint example__
374+
375+
Alternatively, you can also mount the router on `/api`.
341376

342377
```javascript
343378
server.use('/api', router)

0 commit comments

Comments
 (0)