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

fixes example with necessary bodyParser #322

Closed
wants to merge 1 commit into from

Conversation

jpbochi
Copy link

@jpbochi jpbochi commented Jul 4, 2016

I was following the example in the README that has this middleware.

server.use(function (req, res, next) {
  if (req.method === 'POST') {
    req.body.createdAt = Date.now()
  }
  // Continue to JSON Server router
  next()
})

The rewrite has no effect on the behaviour of the server. One thing I noticed is that req.body was always empty by that time. I noticed that it needed to be parsed before the body editing middleware was set.

This problem might have started to happen after some version of express. I'm using 4.13.4, and the code submitted here works fine for me.

@typicode
Copy link
Owner

typicode commented Jul 5, 2016

Good catch, thank you 👍
I'll see if it's possible to fix it directly in json-server.

@jpbochi
Copy link
Author

jpbochi commented Jul 14, 2016

I guess that one solution would be to include body-parser as part of jsonServer.defaults().

@typicode
Copy link
Owner

Yes. The thing I don't know is if there would be some side effects if it's used at the same time in defaults() and router().

@jpbochi
Copy link
Author

jpbochi commented Jul 14, 2016

from what I tested, there was no problem. I'm not sure if express detected that I was repeating a middleware, or if body-parser repeated middleware detected that the body was already parsed and didn't do anything.

It's hard to tell exactly what subtle side-effects could this cause to people that customized json-server. Probably nothing, but you never know what crazy shit people are doing with your open source library. Congrats, by the way. Solid work here.

@typicode
Copy link
Owner

There seems to be some issue for people using http-proxy-middleware #363

So in v0.9.0, which was just released, body-parser is available using jsonServer.bodyParser and the README has been updated:
https://github.com/typicode/json-server#custom-routes-example

@typicode typicode closed this Nov 12, 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 this pull request may close these issues.

2 participants