0
0
NestJSframework~10 mins

Compression and security headers in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable compression middleware in a NestJS application.

NestJS
import * as compression from 'compression';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use([1]());
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
Ahelmet
Bmorgan
Ccors
Dcompression
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'helmet' instead of 'compression' for compression.
Forgetting to call the middleware as a function.
2fill in blank
medium

Complete the code to add security headers using Helmet in a NestJS app.

NestJS
import * as helmet from 'helmet';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use([1]());
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
Ahelmet
Bcompression
Ccors
DbodyParser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'compression' instead of 'helmet' for security headers.
Not calling the middleware function.
3fill in blank
hard

Fix the error in the code to properly apply both compression and helmet middleware.

NestJS
import * as compression from 'compression';
import * as helmet from 'helmet';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(compression());
  app.use([1]);
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
Ahelmet()
Bhelmet
Chelmet.apply()
Dhelmet.use()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'helmet' without parentheses causes runtime errors.
Trying to call non-existent methods like 'apply' or 'use' on helmet.
4fill in blank
hard

Fill both blanks to create a NestJS middleware setup that uses compression and helmet with custom options.

NestJS
import * as compression from 'compression';
import * as helmet from 'helmet';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use([1]({ threshold: 1024 }));
  app.use([2]({ contentSecurityPolicy: false }));
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
Acompression
Bcors
Chelmet
DbodyParser
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up compression and helmet in the wrong blanks.
Using middleware that does not support these options.
5fill in blank
hard

Fill all three blanks to configure a NestJS app with compression, helmet, and CORS middleware with options.

NestJS
import * as compression from 'compression';
import * as helmet from 'helmet';
import * as cors from 'cors';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use([1]({ level: 6 }));
  app.use([2]({ frameguard: { action: 'deny' } }));
  app.use([3]({ origin: 'https://example.com' }));
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
Acompression
Bhelmet
Ccors
DbodyParser
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling middleware functions with parentheses.
Using wrong options for each middleware.