boardgame.io icon indicating copy to clipboard operation
boardgame.io copied to clipboard

Adding middleware causes crash

Open YoavHortman opened this issue 2 years ago • 2 comments

Trying to add a middleware that reads the number of players and getting a crash saying that the stream is already read. This error is because I'm using koaBody() in the middleware to read request.body.numPlayers.

The following line also uses koaBody() but fails, since the stream has already been read. https://github.com/boardgameio/boardgame.io/blob/afc16c6ec20d35665e947894af921893584d1c0f/src/server/api.ts#L233

I suggest making koaBody() happen only if it did not already.

Willing to write the code for this if you think this makes sense.

YoavHortman avatar Oct 31 '22 12:10 YoavHortman

Do I understand correctly that the middleware runs before the router so the body hasn’t been parsed yet? Definitely happy to look at a PR that refactors this! Either by making sure koaBody is called before any middleware to make request.body available everywhere or do what you suggest.

delucis avatar Oct 31 '22 17:10 delucis

Hello, wondering if anyone has worked on this--I have the exact same issue.

kevinddchen avatar Mar 25 '23 06:03 kevinddchen