Add --routes option

This commit is contained in:
Typicode
2015-06-15 18:57:45 +02:00
parent 3479b6a222
commit 5c9309e117
5 changed files with 100 additions and 13 deletions

View File

@ -38,7 +38,7 @@ Also, if you make POST, PUT, PATCH or DELETE requests, changes will be automatic
## Routes
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.
Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-routes).
```
GET /posts
@ -101,13 +101,19 @@ $ npm install -g json-server
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory.
```bash
mkdir public
echo 'hello word' > public/index.html
json-server db.json
```
### Access from anywhere
You can access your fake API from anywhere using CORS and JSONP.
### Remote schema
You can load remote schemas:
You can load remote schemas.
```bash
$ json-server http://example.com/file.json
@ -116,9 +122,12 @@ $ json-server http://jsonplaceholder.typicode.com/db
### JS file support
You can use JS to programmatically create data:
You can create data programmatically.
__Tip__ use modules like [faker](https://github.com/Marak/faker.js) or [casual](https://github.com/boo1ean/casual).
```javascript
// index.js
module.exports = function() {
var data = { users: [] }
// Create 1000 users
@ -133,9 +142,35 @@ module.exports = function() {
$ json-server index.js
```
### Customize
### Add routes
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.
Create a `routes.json` file.
```json
{
"/api/:resource": "/:resource",
"/api/:resource/:id": "/:resource/:id",
"/api/:parent/:parentId/:resource": "/:parent/:parentId/:resource",
"/blog/posts/:id/show": "/posts/:id"
}
```
Start JSON Server with `--routes` option.
```bash
json-server db.json --routes routes.json
```
Now you can access resources using additional routes.
```
/api/posts/1 -> /posts/1
/blog/posts/1/show -> /posts/1
```
### Module
If you need to add authentication, validation, you can use the project as a module in combination with other Express middlewares.
```javascript
var jsonServer = require('json-server')
@ -156,19 +191,17 @@ server.listen(3000)
For an in-memory database, you can pass an object to `jsonServer.router()`.
Please note also that `jsonServer.router()` can be used in existing Express projects.
#### Routes
To add more routes, use redirections or rewrite middlewares.
To add rewrite rules:
```javascript
// Add this before server.use(router)
// Will make /posts/1 available under /blog/posts/1
server.use('/blog/posts/:id', function (req, res) {
res.redirect('/posts/' + req.params.id)
server.use(jsonServer.rewriter({
'/api/:resource': '/:resource',
// ...
})
```
To set a global prefix, mount the router on another point.
To set a global prefix:
```javascript
server.use('/api', router)

View File

@ -30,6 +30,10 @@ var argv = yargs
watch: {
alias: 'w',
description: 'Reload database on JSON file change'
},
routes: {
alias: 'r',
description: 'Load routes file'
}
})
.boolean('w')
@ -60,6 +64,7 @@ function start (object, filename) {
'Enter ' + chalk.cyan('s') + ' at any time to create a snapshot of the db'
)
// Snapshot
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (chunk) {
@ -70,8 +75,14 @@ function start (object, filename) {
}
})
// Rewriter
var routes = JSON.parse(fs.readFileSync(process.cwd() + '/' + argv.routes))
var rewriter = jsonServer.rewriter(routes)
// Router
var router = jsonServer.router(filename ? filename : object)
// Watcher
if (filename && argv.watch) {
console.log('Watching', chalk.cyan(source))
@ -102,6 +113,7 @@ function start (object, filename) {
var server = jsonServer.create()
server.use(jsonServer.defaults)
server.use(rewriter)
server.use(router)
server.listen(port, argv.host)
}

View File

@ -7,5 +7,6 @@ module.exports = {
return server
},
defaults: require('./defaults'),
router: require('./router')
router: require('./router'),
rewriter: require('./rewriter')
}

19
src/rewriter.js Normal file
View File

@ -0,0 +1,19 @@
var express = require('express')
module.exports = function (routes) {
var router = express.Router()
for (var route in routes) {
router.all(route, function (req, res, next) {
var target = routes[route]
for (var param in req.params) {
target = target.replace(':' + param, req.params[param])
}
console.log(target)
req.url = target
next()
})
}
return router
}

View File

@ -39,6 +39,10 @@ describe('Server', function () {
server = jsonServer.create()
router = jsonServer.router(db)
server.use(jsonServer.defaults)
server.use(jsonServer.rewriter({
'/api/:resource/:id': '/:resource/:id',
'/blog/posts/:id/show': '/posts/:id'
}))
server.use(router)
})
@ -363,4 +367,22 @@ describe('Server', function () {
})
})
describe('Rewriter', function () {
it('should rewrite URL (1)', function (done) {
request(server)
.get('/api/posts/1')
.expect(db.posts[0])
.end(done)
})
it('should rewrite URL (2)', function (done) {
request(server)
.get('/blog/posts/1/show')
.expect(db.posts[0])
.end(done)
})
})
})