0
0
NextJSframework~20 mins

Middleware for API routes in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js API middleware?

Consider this Next.js API middleware that adds a custom header before the API handler runs. What will the client receive in the response headers?

NextJS
import { NextResponse } from 'next/server';

export function middleware(request) {
  const response = NextResponse.next();
  response.headers.set('X-Custom-Header', 'HelloWorld');
  return response;
}

export async function GET(request) {
  return NextResponse.json({ message: 'API response' });
}
AThe response will be a 404 error because middleware blocks the handler.
BThe response does not include the custom header because middleware cannot modify headers.
CThe response includes the header 'X-Custom-Header: HelloWorld' along with the JSON message.
DThe response includes the header but with an empty value.
Attempts:
2 left
💡 Hint

Middleware can modify response headers before the handler runs.

📝 Syntax
intermediate
1:30remaining
Which middleware syntax is correct for Next.js API routes?

Identify the correct way to write middleware for Next.js API routes to run before the handler.

Aexport function middleware(req, res, next) { next(); }
Bexport function middleware(request) { return NextResponse.next(); }
Cexport function middleware(req) { return res.next(); }
Dexport default function middleware(req, res) { res.next(); }
Attempts:
2 left
💡 Hint

Next.js middleware uses the NextResponse object and a single request parameter.

🔧 Debug
advanced
2:30remaining
Why does this middleware not modify the API response body?

This middleware tries to add a JSON field to the API response body but it does not work. Why?

NextJS
import { NextResponse } from 'next/server';

export function middleware(request) {
  const response = NextResponse.next();
  const json = JSON.parse(response.body || '{}');
  json.middlewareAdded = true;
  response.body = JSON.stringify(json);
  return response;
}

export async function GET(request) {
  return NextResponse.json({ message: 'Hello' });
}
AThe middleware must use res.json() to modify the body, not NextResponse.
BThe JSON.parse call throws an error because response.body is undefined.
CThe middleware should return a new Response object instead of NextResponse.next().
DMiddleware cannot read or modify the response body because it runs before the handler sends it.
Attempts:
2 left
💡 Hint

Think about when middleware runs relative to the API handler.

state_output
advanced
2:00remaining
What is the value of the custom header after multiple middleware run?

Given two middleware functions that both set the same header, what will the client receive?

NextJS
import { NextResponse } from 'next/server';

export function middleware(request) {
  const response = NextResponse.next();
  response.headers.set('X-Trace-Id', 'first');
  return response;
}

export function secondMiddleware(request) {
  const response = NextResponse.next();
  response.headers.set('X-Trace-Id', 'second');
  return response;
}
AThe client receives 'X-Trace-Id: second' because the last middleware overwrites the header.
BThe client receives 'X-Trace-Id: first' because only the first middleware header is kept.
CThe client receives both headers as a comma-separated list.
DThe client receives no 'X-Trace-Id' header because of conflict.
Attempts:
2 left
💡 Hint

Headers with the same name get overwritten by later middleware.

🧠 Conceptual
expert
2:30remaining
Which statement about Next.js API middleware is true?

Choose the correct statement about how Next.js middleware works with API routes.

AMiddleware runs before the API route handler and can modify request headers but not the response body.
BMiddleware can replace the API route handler by returning a custom response.
CMiddleware runs only on client-side navigation and does not affect API routes.
DMiddleware runs after the API route handler and can modify the response body before sending.
Attempts:
2 left
💡 Hint

Think about the middleware lifecycle and what it can access.