react-router icon indicating copy to clipboard operation
react-router copied to clipboard

feat(express): add support for readable stream already closed

Open HenriqueLimas opened this issue 4 months ago • 3 comments

Description

This change resolves an issue where the request body is inaccessible to react-router when a Node.js server, such as Express, uses middleware that consumes the request stream before it reaches the router's handler.

Fixes: https://github.com/remix-run/remix/discussions/10132

The Problem

In Node.js applications, it's common to use middleware like express.json() or express.urlencoded() to parse the request body. This process reads and consumes the request's ReadableStream.

Since Node.js streams cannot be consumed more than once (unlike the Web API's ReadableStream which can be cloned using the .tee() method), react-router is subsequently unable to read the request body. This is a frequent issue in applications that layer react-router with other Express middleware.

This PR introduces a new check to verify if the request stream has already been closed. If it has, the handler will now fallback to using the body that was already parsed and it transform it to string.

HenriqueLimas avatar Jul 15 '25 00:07 HenriqueLimas