Skip to content

Commit 2c6fbf3

Browse files
committed
Refactor
1 parent ffda723 commit 2c6fbf3

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* Improve performances
6+
* Add `Location` header to newly created resources [#473](https://github.com/typicode/json-server/pull/473)
67

78
## [0.9.5][2016-02-11]
89

@@ -12,7 +13,7 @@
1213
## [0.9.4][2016-12-08]
1314

1415
* Improve rewriter [#431](https://github.com/typicode/json-server/issues/431)
15-
* Improve watch mode [#427](https://github.com/typicode/json-server/pull/427)
16+
* Improve watch mode [#427](https://github.com/typicode/json-server/pull/427)
1617

1718
## [0.9.3][2016-12-07]
1819

@@ -39,7 +40,7 @@
3940
* [#363](https://github.com/typicode/json-server/issues/363) [#365](https://github.com/typicode/json-server/issues/365)
4041
* [#374](https://github.com/typicode/json-server/issues/374)
4142
* [#383](https://github.com/typicode/json-server/issues/383)
42-
* Updated dependencies and codebase to ES6
43+
* Updated dependencies and codebase to ES6
4344

4445
## [0.8.23][2016-11-03]
4546

src/server/router/get-full-url.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const url = require('url')
2+
3+
module.exports = function getFullURL (req) {
4+
const root = url.format({
5+
protocol: req.protocol,
6+
host: req.get('host')
7+
})
8+
9+
return `${root}${req.originalUrl}`
10+
}

src/server/router/plural.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const url = require('url')
21
const express = require('express')
32
const _ = require('lodash')
43
const pluralize = require('pluralize')
54
const write = require('./write')
5+
const getFullURL = require('./get-full-url')
66
const utils = require('../utils')
77

88
module.exports = (db, name) => {
@@ -34,15 +34,6 @@ module.exports = (db, name) => {
3434
})
3535
}
3636

37-
function getFullURL (req) {
38-
const root = url.format({
39-
protocol: req.protocol,
40-
host: req.get('host')
41-
})
42-
43-
return `${root}${req.originalUrl}`
44-
}
45-
4637
// GET /name
4738
// GET /name?q=
4839
// GET /name?attr=&attr=
@@ -250,7 +241,8 @@ module.exports = (db, name) => {
250241
.value()
251242

252243
res.setHeader('Access-Control-Expose-Headers', 'Location')
253-
res.setHeader('Location', getFullURL(req) + '/' + resource.id)
244+
res.location(`${getFullURL(req)}/${resource.id}`)
245+
254246
res.status(201)
255247
res.locals.data = resource
256248

src/server/router/singular.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require('express')
22
const write = require('./write')
3+
const getFullURL = require('./get-full-url')
34

45
module.exports = (db, name) => {
56
const router = express.Router()
@@ -12,6 +13,10 @@ module.exports = (db, name) => {
1213
function create (req, res, next) {
1314
db.set(name, req.body).value()
1415
res.locals.data = db.get(name).value()
16+
17+
res.setHeader('Access-Control-Expose-Headers', 'Location')
18+
res.location(`${getFullURL(req)}`)
19+
1520
res.status(201)
1621
next()
1722
}

test/server/plural.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ describe('Server', () => {
506506
request(server)
507507
.post('/posts')
508508
.send({body: 'foo', booleanValue: true, integerValue: 1})
509-
.expect('Location', /3$/)
509+
.expect('Access-Control-Expose-Headers', 'Location')
510+
.expect('Location', /posts\/3$/)
510511
.expect('Content-Type', /json/)
511512
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
512513
.expect(201)

0 commit comments

Comments
 (0)