Skip to content

Commit b80f4ed

Browse files
committed
Update README.md
1 parent df7a0ac commit b80f4ed

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Also, if you make POST, PUT, PATCH or DELETE requests, changes will be automatic
3838

3939
## Routes
4040

41-
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).
41+
Based on the previous `db.json` file, here are all the available routes. If you need to [customize](https://github.com/typicode/json-server#customize), you can use the project as a module.
4242

4343
```
4444
GET /posts
@@ -133,18 +133,30 @@ module.exports = function() {
133133
$ json-server index.js
134134
```
135135

136-
### Module
136+
### Customize
137137

138138
If you need to add authentication, validation, rewrite or add routes, you can use the project as a module in combination with other Express middlewares.
139139

140140
```javascript
141141
var jsonServer = require('json-server')
142142

143-
var server = jsonServer.create() // Returns an Express server
144-
var router = jsonServer.router('db.json') // Returns an Express router
143+
// Returns an Express server
144+
var server = jsonServer.create()
145145

146-
server.use(jsonServer.defaults) // logger, static and cors middlewares
147-
server.use(router) // Mount router on '/'
146+
// Returns an Express router
147+
var router = jsonServer.router('db.json')
148+
149+
// Default middlewares (logger, static and cors middlewares)
150+
server.use(jsonServer.defaults)
151+
152+
// Add custom routes (optional)
153+
// You can use rewrite middlewares if you don't want HTTP redirections
154+
server.use('/custom/route/:id', function (req, res) {
155+
res.redirect('/posts/' + req.params.id)
156+
})
157+
158+
// Mount router on '/'
159+
server.use(router)
148160

149161
server.listen(3000)
150162
```

0 commit comments

Comments
 (0)