Consider a NestJS application where compression middleware is enabled. What will be the visible effect on the HTTP responses sent to clients?
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import * as compression from 'compression'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(compression()); await app.listen(3000); } bootstrap();
Think about what compression middleware does to the data sent over the network.
Compression middleware compresses the HTTP response body (e.g., using gzip) to reduce the size of data sent to clients, improving load times and bandwidth usage.
Given the following code snippet, which option correctly applies Helmet middleware to add security headers?
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import * as helmet from 'helmet'; async function bootstrap() { const app = await NestFactory.create(AppModule); // Apply helmet middleware here await app.listen(3000); } bootstrap();
Remember how to apply middleware functions in Express-based NestJS apps.
Helmet is a middleware function. To apply it, you call app.use(helmet()). Other options are invalid method calls or missing parentheses.
Review the code below. The developer expects responses to be compressed but they are not. What is the cause?
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import * as compression from 'compression'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(compression); await app.listen(3000); } bootstrap();
Check how middleware functions are passed to app.use.
Middleware like compression must be called as a function to return the middleware handler. Passing compression without parentheses means the middleware is not applied.
After applying Helmet middleware with app.use(helmet()), which of the following headers will be present in HTTP responses by default?
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import * as helmet from 'helmet'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(helmet()); await app.listen(3000); } bootstrap();
Think about common security headers that protect against clickjacking, sniffing, and other attacks.
Helmet sets several security headers by default, including X-Frame-Options, Strict-Transport-Security, X-Content-Type-Options, and others to improve security.
Choose the best explanation for why a NestJS app should use both compression middleware and security headers middleware like Helmet together.
Think about the different roles compression and security headers play in web apps.
Compression improves performance by reducing data size. Security headers protect users from common web attacks. Using both ensures the app is fast and safe.