1require("reflect-metadata");
2const { NestFactory } = require("@nestjs/core");
3const { Controller, Module, Body, Post, Req } = require("@nestjs/common");
4
5class C {
6 @Post("/hook")
7 hook(@Req() req, @Body() b) {
8 return {
9 rawBodyIsBuffer: Buffer.isBuffer(req.rawBody),
10 rawBodyLen: req.rawBody ? req.rawBody.length : null,
11 bodyKeys: b ? Object.keys(b) : null,
12 bodyBlobLen: b && b.blob ? b.blob.length : null
13 };
14 }
15}
16@Module({ controllers: [C] })
17class M {}
18
19(async () => {
20 const app = await NestFactory.create(M, { logger: false, rawBody: true });
21 const server = await app.listen(0);
22 const port = server.address().port;
23 const small = JSON.stringify({ hello: "world", n: 123 });
24 const r1 = await fetch(`http:
25 console.log("small ->", r1.status, await r1.text());
26 const big = JSON.stringify({ blob: "x".repeat(150 * 1024) });
27 const r2 = await fetch(`http:
28 console.log("150kb ->", r2.status, (await r2.text()).slice(0, 140));
29 await app.close();
30})();
31