From 6dc998da3a0d747bf4c3b27cf24ef06636a1103b Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Wed, 5 Nov 2025 17:17:54 +0000 Subject: [PATCH] Fix raw body capture middleware cgen-2d2ac1f9fd1540f984ef7323bca961f0 --- server/index.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/server/index.ts b/server/index.ts index 02dd0fdb..ee19dfc8 100644 --- a/server/index.ts +++ b/server/index.ts @@ -8,15 +8,21 @@ import { randomUUID, createHash, createVerify } from "crypto"; export function createServer() { const app = express(); - // Middleware - capture raw body for Discord signature verification - app.use((req, res, buf, encoding) => { - if (buf && buf.length) { - req.rawBody = buf.toString(encoding || 'utf8'); - } - }); - // Middleware app.use(cors()); + + // Capture raw body for Discord signature verification + app.use((req, res, next) => { + let rawBody = ''; + req.on('data', (chunk) => { + rawBody += chunk.toString('utf8'); + }); + req.on('end', () => { + (req as any).rawBody = rawBody; + next(); + }); + }); + app.use(express.json()); app.use(express.urlencoded({ extended: true }));