mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 06:34:02 +08:00
Add POST /:resource/:id/:nested route
This commit is contained in:
@ -1,5 +1,12 @@
|
|||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## [0.8.3][2015-11-25]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* CLI option `-q/--quied`
|
||||||
|
* Nested route `POST /posts/1/comments`
|
||||||
|
|
||||||
## [0.8.2][2015-10-15]
|
## [0.8.2][2015-10-15]
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@ -130,10 +130,11 @@ GET /comments?_expand=post
|
|||||||
GET /comments/1?_expand=post
|
GET /comments/1?_expand=post
|
||||||
```
|
```
|
||||||
|
|
||||||
To get nested resources (by default one level, [add routes](#add-routes) for more)
|
To get or create nested resources (by default one level, [add routes](#add-routes) for more)
|
||||||
|
|
||||||
```
|
```
|
||||||
GET /posts/1/comments
|
GET /posts/1/comments
|
||||||
|
POST /posts/1/comments
|
||||||
```
|
```
|
||||||
|
|
||||||
### Database
|
### Database
|
||||||
|
@ -6,13 +6,23 @@ module.exports = function () {
|
|||||||
|
|
||||||
var router = express.Router()
|
var router = express.Router()
|
||||||
|
|
||||||
// Rewrite url to /:nested?:resourceId=:id
|
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request query
|
||||||
router.get('/:resource/:id/:nested', function (req, res, next) {
|
function get (req, res, next) {
|
||||||
var prop = pluralize.singular(req.params.resource)
|
var prop = pluralize.singular(req.params.resource)
|
||||||
req.query[prop + 'Id'] = utils.toNative(req.params.id)
|
req.query[prop + 'Id'] = utils.toNative(req.params.id)
|
||||||
req.url = '/' + req.params.nested
|
req.url = '/' + req.params.nested
|
||||||
next()
|
next()
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// Rewrite URL (/:resource/:id/:nested -> /:nested) and request body
|
||||||
|
function post (req, res, next) {
|
||||||
|
var prop = pluralize.singular(req.params.resource)
|
||||||
|
req.body[prop + 'Id'] = utils.toNative(req.params.id)
|
||||||
|
req.url = '/' + req.params.nested
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
|
||||||
return router
|
return router
|
||||||
|
.get('/:resource/:id/:nested', get)
|
||||||
|
.post('/:resource/:id/:nested', post)
|
||||||
}
|
}
|
||||||
|
@ -407,6 +407,17 @@ describe('Server', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('POST /:parent/:parentId/:resource', function () {
|
||||||
|
it('should respond with json and set parentId', function (done) {
|
||||||
|
request(server)
|
||||||
|
.post('/posts/1/comments')
|
||||||
|
.send({body: 'foo'})
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect({id: 6, postId: 1, body: 'foo'})
|
||||||
|
.expect(201, done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('PUT /:resource/:id', function () {
|
describe('PUT /:resource/:id', function () {
|
||||||
it('should respond with json and replace resource', function (done) {
|
it('should respond with json and replace resource', function (done) {
|
||||||
var post = {id: 1, booleanValue: true, integerValue: 1}
|
var post = {id: 1, booleanValue: true, integerValue: 1}
|
||||||
|
Reference in New Issue
Block a user