I’m encountering an issue with the compression middleware in my NestJS project. I followed the documentation provided here and installed compression using Yarn. However, I noticed a couple of discrepancies between the documentation and my implementation:
- Importing the Package: In the documentation, the package is imported using
import * as compression from 'compression';
, but when I tried this, I received aTypeError stating that compression is not a function
. Instead, I had to useimport compression from 'compression';
. Is this discrepancy significant? - Effectiveness of Compression: After adding the compression middleware to my project’s middlewares (
app.use(compression({ level: 6, threshold: 0 }));
), I tested its effectiveness by returning a large string from one of my endpoints (return 'response'.repeat(1000)
). While I can see the gzip header added to the response, the response size remains unchanged whether compression is enabled or not. Even when I curl the endpoint, I still receive the plaintext response along with the gzip header.
Why isn’t the response size changing despite the gzip header being added, and is my implementation of the compression middleware correct given the differences in importing the package?
Any insights or suggestions would be greatly appreciated.
With compression:
Without compression