Search code examples
javascriptexpresscompression

How do you compress your response with express.js?


I am currently using the latest express and compression. I am reading a text file and returning it with compression enabled.

const express = require('express')
const app = express()
const port = 3000
var compression = require('compression')

app.use(compression())

app.get('/', (req, res) => {
    var fs = require('fs')
    fs.readFile('./test.txt', 'utf8', function(err, data) {
        if (err) throw err;
        res.send(data)
    });
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

The app reads and return the text content properly when I pass the header Accept-Encoding: gzip, deflate. Even though the response headers says the response is Content-Encoding: gzip, the size is similar to the size of the text file. Am I missing anything to compress the response properly?


Solution

  • Turns out it is an existing bug in the latest Postman as of May 1st, 2023 that does not show the compressed response size when it is returned from nodejs. When I use the Insomnia app, I can see the compressed response size for the same code.