diff --git a/CHANGELOG.md b/CHANGELOG.md index dda9725..004794a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased * Improve performances +* Add `Location` header to newly created resources [#473](https://github.com/typicode/json-server/pull/473) ## [0.9.5][2016-02-11] @@ -12,7 +13,7 @@ ## [0.9.4][2016-12-08] * Improve rewriter [#431](https://github.com/typicode/json-server/issues/431) -* Improve watch mode [#427](https://github.com/typicode/json-server/pull/427) +* Improve watch mode [#427](https://github.com/typicode/json-server/pull/427) ## [0.9.3][2016-12-07] @@ -39,7 +40,7 @@ * [#363](https://github.com/typicode/json-server/issues/363) [#365](https://github.com/typicode/json-server/issues/365) * [#374](https://github.com/typicode/json-server/issues/374) * [#383](https://github.com/typicode/json-server/issues/383) -* Updated dependencies and codebase to ES6 +* Updated dependencies and codebase to ES6 ## [0.8.23][2016-11-03] diff --git a/src/server/router/get-full-url.js b/src/server/router/get-full-url.js new file mode 100644 index 0000000..19cd41b --- /dev/null +++ b/src/server/router/get-full-url.js @@ -0,0 +1,10 @@ +const url = require('url') + +module.exports = function getFullURL (req) { + const root = url.format({ + protocol: req.protocol, + host: req.get('host') + }) + + return `${root}${req.originalUrl}` +} diff --git a/src/server/router/plural.js b/src/server/router/plural.js index 138b455..3090989 100644 --- a/src/server/router/plural.js +++ b/src/server/router/plural.js @@ -1,8 +1,8 @@ -const url = require('url') const express = require('express') const _ = require('lodash') const pluralize = require('pluralize') const write = require('./write') +const getFullURL = require('./get-full-url') const utils = require('../utils') module.exports = (db, name) => { @@ -34,15 +34,6 @@ module.exports = (db, name) => { }) } - function getFullURL (req) { - const root = url.format({ - protocol: req.protocol, - host: req.get('host') - }) - - return `${root}${req.originalUrl}` - } - // GET /name // GET /name?q= // GET /name?attr=&attr= @@ -250,7 +241,8 @@ module.exports = (db, name) => { .value() res.setHeader('Access-Control-Expose-Headers', 'Location') - res.setHeader('Location', getFullURL(req) + '/' + resource.id) + res.location(`${getFullURL(req)}/${resource.id}`) + res.status(201) res.locals.data = resource diff --git a/src/server/router/singular.js b/src/server/router/singular.js index 34a6d2e..0ef3356 100644 --- a/src/server/router/singular.js +++ b/src/server/router/singular.js @@ -1,5 +1,6 @@ const express = require('express') const write = require('./write') +const getFullURL = require('./get-full-url') module.exports = (db, name) => { const router = express.Router() @@ -12,6 +13,10 @@ module.exports = (db, name) => { function create (req, res, next) { db.set(name, req.body).value() res.locals.data = db.get(name).value() + + res.setHeader('Access-Control-Expose-Headers', 'Location') + res.location(`${getFullURL(req)}`) + res.status(201) next() } diff --git a/test/server/plural.js b/test/server/plural.js index 3b75253..5ed3386 100644 --- a/test/server/plural.js +++ b/test/server/plural.js @@ -506,7 +506,8 @@ describe('Server', () => { request(server) .post('/posts') .send({body: 'foo', booleanValue: true, integerValue: 1}) - .expect('Location', /3$/) + .expect('Access-Control-Expose-Headers', 'Location') + .expect('Location', /posts\/3$/) .expect('Content-Type', /json/) .expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1}) .expect(201)