CodeGenieApp / CodeGenieApp/serverless-express

Express v5 and body-parser v2 not compatible with serverless-express

Abierto
#515 3 comentarios 5 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
JavaScript
Estrellas
5.3k
Forks
676
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Hello! It seems like express v5 and its bundled body-parser v2 do not work with serverless-express. The body-parser dependency has changed its internals to use "on-finished" dependency to check if the body is parsed already. It does so by checking if the request stream has "completed" set to true. The requests from serverless-express are always "completed" when it reaches the express app, i.e. the body-parser thinks the body is already parsed and skips parsing e.g. JSON content.

Code that always sets request to completed:
https://github.com/vendia/serverless-express/blob/f3f9c50f6dfcdbaf84af6ca64e77af1ba8b34bf0/src/request.js#L20

The "on-finished" code that checks if the request body is already parsed:
https://github.com/jshttp/on-finished/blob/1111fe8e913debaf3da9bd4f6bda216ef36097fa/index.js#L76

Here's the change in body-parser which start using the "on-finished":
https://github.com/expressjs/body-parser/compare/1.20.0...v2.0.0-beta.1#diff-c0749b151a7ec0288a6a4c849ff5215ec9ddeef8fa799a5cecef4b320a7dcf00R100

Here's example code:
```
import express from 'express'; // ^5.0.0-beta.1
import serverlessExpress from '@vendia/serverless-express'; // ^4.8.0

const app = express();
app.use(express.json());
app.post('/example', (req, res) => {
console.log('body', req.body);
})

export const handler = serverlessExpress({ app });
```

Calling POST /example prints a "Buffer" object. If you enable debugging for body-parser (set env flag DEBUG=body-parser:*), then it prints out "body-parser:json body already parsed".

How can this be solved? I am afraid that if the request has "completed" to false, it might have unexpected consequences as well.

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Start with src/request.js, especially the code that marks the request completed, and reproduce the provided Express 5 example with express.json(). Compare that behavior with the referenced on-finished and body-parser changes. Done means JSON requests are parsed by Express 5 instead of arriving as a Buffer, without introducing request-handling regressions.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
express, javascript, node.js
Área
api, backend, cloud
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.