Bird
Raised Fist0
Node.jsframework~20 mins

Compression middleware in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Compression Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output behavior of this Express app using compression middleware?

Consider this Express.js server code snippet using compression middleware:

import express from 'express';
import compression from 'compression';

const app = express();
app.use(compression());

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000);

What happens when a client sends a request with Accept-Encoding: gzip header?

Node.js
import express from 'express';
import compression from 'compression';

const app = express();
app.use(compression());

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000);
AThe server responds with compressed content but does not set any Content-Encoding header.
BThe server responds with uncompressed 'Hello World!' ignoring the Accept-Encoding header.
CThe server throws an error because compression middleware requires manual compression calls.
DThe server responds with compressed (gzip) 'Hello World!' content and sets 'Content-Encoding: gzip' header.
Attempts:
2 left
💡 Hint

Compression middleware automatically compresses responses when the client supports it.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies compression middleware only to JSON responses?

You want to compress only JSON responses in your Express app. Which code snippet correctly achieves this?

Aapp.use(compression({ filter: (req, res) => res.getHeader('Content-Type') === 'application/json' }));
Bapp.use(compression({ filter: (req, res) => res.getHeader('content-type')?.includes('application/json') }));
Capp.use(compression({ filter: (req, res) => req.headers['content-type'] === 'application/json' }));
Dapp.use(compression({ filter: (req, res) => res.headers['Content-Type'] === 'application/json' }));
Attempts:
2 left
💡 Hint

Headers are case-insensitive and may include charset info.

🔧 Debug
advanced
2:00remaining
Why does this Express app fail to compress responses?

Look at this Express app code:

import express from 'express';
import compression from 'compression';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.use(compression());

app.listen(3000);

Why are responses not compressed?

Node.js
import express from 'express';
import compression from 'compression';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.use(compression());

app.listen(3000);
ACompression middleware is added after routes, so it does not affect responses.
BCompression middleware only works with POST requests.
CThe app is missing a call to next() in the route handler.
DCompression middleware requires manual calls to compress responses.
Attempts:
2 left
💡 Hint

Middleware order matters in Express.

🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using compression middleware in a Node.js server?

Why do developers add compression middleware like compression in their Node.js servers?

ATo reduce the size of response bodies, improving load times and saving bandwidth.
BTo encrypt response data for security purposes.
CTo cache responses on the server for faster repeated requests.
DTo convert all responses to JSON format automatically.
Attempts:
2 left
💡 Hint

Think about what compression means in everyday life.

❓ state_output
expert
2:00remaining
What is the Content-Encoding header value after this code runs?

Given this Express app snippet:

import express from 'express';
import compression from 'compression';

const app = express();
app.use(compression({ threshold: 1024 }));

app.get('/', (req, res) => {
  res.send('Short');
});

app.listen(3000);

If a client requests '/' with Accept-Encoding: gzip, what is the Content-Encoding header in the response?

Node.js
import express from 'express';
import compression from 'compression';

const app = express();
app.use(compression({ threshold: 1024 }));

app.get('/', (req, res) => {
  res.send('Short');
});

app.listen(3000);
AContent-Encoding: br (Brotli) is set automatically.
BContent-Encoding: deflate is set by default.
CNo Content-Encoding header is set because response size is below threshold.
DContent-Encoding: gzip is set because client accepts gzip.
Attempts:
2 left
💡 Hint

Check the meaning of the threshold option in compression middleware.

Practice

(1/5)
1. What is the main purpose of using compression middleware in a Node.js Express app?
easy
A. To parse JSON data from client requests
B. To add security headers to HTTP responses
C. To log all incoming requests to the server
D. To reduce the size of server responses and speed up loading times

Solution

  1. Step 1: Understand what compression middleware does

    Compression middleware compresses the data sent from the server to the client to reduce its size.
  2. Step 2: Identify the benefit of compression

    Smaller response sizes mean faster loading times for users because less data travels over the network.
  3. Final Answer:

    To reduce the size of server responses and speed up loading times -> Option D
  4. Quick Check:

    Compression = smaller responses = faster loading [OK]
Hint: Compression shrinks response size to speed up loading [OK]
Common Mistakes:
  • Confusing compression with security or logging
  • Thinking compression parses request data
  • Assuming compression adds headers unrelated to size
2. Which of the following is the correct way to add compression middleware in an Express app?
easy
A. app.use('compression');
B. app.use(compression);
C. app.use(compression());
D. app.use(compress());

Solution

  1. Step 1: Recall the syntax for middleware usage in Express

    Middleware functions are passed as functions, so you must call compression() to get the middleware function.
  2. Step 2: Identify the correct syntax

    app.use(compression()); correctly calls the compression function and passes the middleware to Express.
  3. Final Answer:

    app.use(compression()); -> Option C
  4. Quick Check:

    Middleware needs function call syntax () [OK]
Hint: Middleware must be called as a function with () [OK]
Common Mistakes:
  • Forgetting parentheses after compression
  • Passing string instead of function
  • Using wrong function name like compress
3. Given this Express code snippet, what will be the effect on the response?
import express from 'express';
import compression from 'compression';

const app = express();
app.use(compression());

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000);
medium
A. The response will be compressed if the client supports it
B. The response will always be sent uncompressed
C. The server will throw an error because compression is not configured
D. The response will be compressed only for POST requests

Solution

  1. Step 1: Understand compression middleware behavior

    Compression middleware compresses responses only if the client sends an Accept-Encoding header that supports compression (like gzip).
  2. Step 2: Analyze the code snippet

    The middleware is applied globally with app.use(compression()), so all responses, including GET '/', will be compressed if the client supports it.
  3. Final Answer:

    The response will be compressed if the client supports it -> Option A
  4. Quick Check:

    Compression depends on client support [OK]
Hint: Compression works only if client accepts it [OK]
Common Mistakes:
  • Assuming compression always compresses regardless of client
  • Thinking compression only works on POST
  • Believing compression requires extra config to avoid errors
4. What is wrong with this code snippet that tries to add compression middleware?
const express = require('express');
const compression = require('compression');

const app = express();
app.use(compression);

app.get('/', (req, res) => {
  res.send('Hello');
});

app.listen(3000);
medium
A. The route handler is missing a next() call
B. Compression middleware is not called as a function
C. Express app is not created correctly
D. Compression middleware must be added after routes

Solution

  1. Step 1: Check how compression middleware is added

    The code uses app.use(compression); which passes the compression function itself, not the middleware returned by calling it.
  2. Step 2: Identify correct usage

    Compression middleware must be called as compression() to return the middleware function, so it should be app.use(compression());.
  3. Final Answer:

    Compression middleware is not called as a function -> Option B
  4. Quick Check:

    Middleware must be called with () [OK]
Hint: Always call middleware functions with () when adding [OK]
Common Mistakes:
  • Passing middleware function without calling it
  • Thinking middleware order doesn't matter here
  • Confusing route handler requirements with middleware
5. You want to compress only responses larger than 1KB in your Express app. Which code snippet correctly applies this condition using compression middleware?
hard
A. app.use(compression({ threshold: 1024 }));
B. app.use(compression({ minSize: 1024 }));
C. app.use(compression({ sizeLimit: 1024 }));
D. app.use(compression({ compressAbove: 1024 }));

Solution

  1. Step 1: Recall compression middleware options

    The official compression middleware option to set minimum response size for compression is threshold, which accepts bytes.
  2. Step 2: Match the correct option name and value

    Setting threshold: 1024 means compress responses only if size is greater than 1024 bytes (1KB).
  3. Final Answer:

    app.use(compression({ threshold: 1024 })); -> Option A
  4. Quick Check:

    Use threshold option to set minimum size [OK]
Hint: Use threshold option to set minimum compression size [OK]
Common Mistakes:
  • Using incorrect option names like minSize or sizeLimit
  • Not specifying size in bytes
  • Assuming compression applies to all sizes by default
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard