CodeGenieApp / CodeGenieApp/serverless-express

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

Đang mở
#515 3 bình luận 5 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
5.3k
Fork
676
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu với src/request.js, đặc biệt là phần mã đánh dấu request đã hoàn tất, và tái hiện ví dụ Express 5 được cung cấp với express.json(). So sánh hành vi đó với các thay đổi được tham chiếu trong on-finished và body-parser. Công việc được xem là hoàn tất khi các request JSON được Express 5 phân tích thay vì đến dưới dạng Buffer, mà không gây ra hồi quy trong việc xử lý request.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
express, javascript, node.js
Lĩnh vực
api, backend, cloud
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.