Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routes display incorrect when custom wrap response #341

Closed
tao12345666333 opened this issue Aug 10, 2016 · 2 comments · Fixed by #350
Closed

Routes display incorrect when custom wrap response #341

tao12345666333 opened this issue Aug 10, 2016 · 2 comments · Fixed by #350

Comments

@tao12345666333
Copy link
Contributor

When I custom wrap response, the server index routes display incorrect.

My server.js code

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)

server.listen(3000, function () {
  console.log('JSON Server is running')
})

router.render = function (req, res) {
  res.jsonp({
    body: res.locals.data,
    code: 0,
    msg: '',
  })
}

The response is correct.
84181101-ad0a-463a-8e55-37d21bf9d179

But when I open http://127.0.0.1:3000 the page display:
f3477d5c-9206-4acd-9a76-c3d875e13b3f

My env is:

(Tao) ➜  api git:(master) ✗ json-server --version
0.8.17
(Tao) ➜  api git:(master) ✗ node --version
v6.2.1

Thanks for your json-server.

@typicode
Copy link
Owner

Hi @tao12345666333,

Good catch, it's a bug here:
https://github.com/typicode/json-server/blob/master/src/server/router/index.js#L48

next() shouldn't be called, instead db state should be returned immediately (res.jsonp(state)) to avoid going through the custom wrapper.

Do you want to make a PR?

@tao12345666333
Copy link
Contributor Author

Yes, I will do it.

tao12345666333 added a commit to tao12345666333/json-server that referenced this issue Aug 30, 2016
typicode pushed a commit that referenced this issue Aug 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants