What is the purpose of compression middleware in a NestJS application?
Compression middleware reduces the size of the response body sent to clients. This makes data transfer faster and saves bandwidth, improving app performance.
Click to reveal answer
beginner
Name a common security header that helps prevent cross-site scripting (XSS) attacks.
The Content-Security-Policy (CSP) header helps prevent XSS by specifying which sources of content are allowed to load on the page.
Click to reveal answer
intermediate
How do you enable compression in a NestJS app using middleware?
You can enable compression by installing the 'compression' package and applying it as middleware in the main app file using app.use(compression()).
Click to reveal answer
intermediate
What does the 'helmet' package do in a NestJS project?
Helmet sets various HTTP headers to secure your app from common vulnerabilities like clickjacking, MIME sniffing, and XSS.
Click to reveal answer
beginner
Why is it important to set security headers in your NestJS backend?
Security headers protect users by controlling browser behavior, reducing risks like data theft, code injection, and clickjacking.
Click to reveal answer
Which package is commonly used to add compression middleware in NestJS?
Aexpress-session
Bhelmet
Ccors
Dcompression
✗ Incorrect
The 'compression' package is used to compress response bodies in NestJS.
What does the 'helmet' package primarily help with?
AAdding security headers
BCompressing responses
CManaging sessions
DHandling CORS
✗ Incorrect
Helmet sets security-related HTTP headers to protect the app.
Which security header helps prevent clickjacking attacks?
AX-Frame-Options
BContent-Type
CCache-Control
DAuthorization
✗ Incorrect
X-Frame-Options prevents your site from being framed by other sites, blocking clickjacking.
How do you apply compression middleware in a NestJS app?
Aapp.set('compression', true)
Bapp.enableCompression()
Capp.use(compression())
Dapp.useHelmet()
✗ Incorrect
You apply compression middleware by calling app.use(compression()).
Which header controls which external resources can be loaded on a page?
AX-Powered-By
BContent-Security-Policy
CETag
DAccept-Encoding
✗ Incorrect
Content-Security-Policy defines allowed sources for scripts, styles, and other resources.
Explain how to add compression and security headers in a NestJS application.
Think about middleware setup in main.ts and what each package does.
You got /4 concepts.
Describe why security headers are important and name three common headers used in NestJS apps.
Consider how browsers behave with these headers.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using compression middleware in a NestJS application?
easy
A. To reduce the size of response data sent to clients
B. To add security headers to HTTP responses
C. To log incoming requests for debugging
D. To handle database connections efficiently
Solution
Step 1: Understand what compression middleware does
The compression middleware compresses HTTP response bodies to reduce their size.
Step 2: Identify the effect on client-server communication
Smaller response sizes mean faster data transfer and improved app speed.
Final Answer:
To reduce the size of response data sent to clients -> Option A
Quick Check:
Compression = smaller response size [OK]
Hint: Compression shrinks data sent to users for speed [OK]
Common Mistakes:
Confusing compression with security features
Thinking compression logs requests
Assuming compression manages databases
2. Which of the following is the correct way to apply helmet middleware in a NestJS main.ts file?
easy
A. app.apply(helmet);
B. app.helmet();
C. helmet(app);
D. app.use(helmet());
Solution
Step 1: Recall the NestJS middleware usage pattern
Middleware is applied using app.use() with the middleware function as argument.
Step 2: Check the syntax for helmet middleware
The correct syntax is app.use(helmet()); to apply helmet with default settings.
Final Answer:
app.use(helmet()); -> Option D
Quick Check:
Middleware applied with app.use() [OK]
Hint: Use app.use() to add middleware like helmet [OK]
Common Mistakes:
Calling helmet as a method on app
Passing app as argument to helmet
Using app.apply() which is invalid
3. Given this NestJS setup in main.ts:
import * as compression from 'compression';
import * as helmet from 'helmet';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(compression());
app.use(helmet());
await app.listen(3000);
}
What will be the effect on HTTP responses?
medium
A. No effect; middleware is not applied correctly
B. Responses will be compressed and have security headers added
C. Responses will only have security headers, no compression
D. Responses will only be compressed, no security headers
Solution
Step 1: Analyze middleware usage in the code
Both compression() and helmet() are applied using app.use(), which is correct.
Responses will be compressed and have security headers added -> Option B
Quick Check:
Both compression and helmet applied = compressed + secure headers [OK]
Hint: Both middlewares applied means both effects present [OK]
Common Mistakes:
Thinking only one middleware works at a time
Assuming middleware needs special chaining
Believing middleware is ignored without extra config
4. Identify the error in this NestJS main.ts snippet:
import helmet from 'helmet';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(helmet);
await app.listen(3000);
}
medium
A. helmet is not called as a function before passing to app.use
B. helmet should be imported with * as helmet from 'helmet'
C. app.listen should be called before app.use
D. NestFactory.create requires an options object
Solution
Step 1: Check how helmet middleware is passed
Middleware functions must be called to return a function; here helmet is passed without parentheses.
Step 2: Confirm correct usage
The correct usage is app.use(helmet()); to apply helmet middleware.
Final Answer:
helmet is not called as a function before passing to app.use -> Option A
Quick Check:
Middleware must be called: helmet() [OK]
Hint: Always call middleware functions before app.use [OK]
Common Mistakes:
Passing middleware reference without calling it
Importing helmet incorrectly but that still works with default export
Calling app.listen before middleware setup
5. You want to add compression and security headers in a NestJS app but only compress responses larger than 1KB and disable the X-Powered-By header. Which setup achieves this?
hard
A. app.use(compression({ minSize: 1024 })); app.use(helmet({ hidePoweredBy: true }));
B. app.use(compression({ threshold: 1024 })); app.use(helmet.hidePoweredBy());
C. app.use(compression({ threshold: 1024 })); app.use(helmet({ hidePoweredBy: true }));
D. app.use(compression()); app.use(helmet({ disablePoweredBy: true }));
Solution
Step 1: Configure compression threshold correctly
The compression middleware option to set minimum size is threshold, so { threshold: 1024 } means compress responses over 1KB.
Step 2: Disable X-Powered-By header using helmet option
Helmet's option to hide this header is hidePoweredBy: true.