Chapter 16 of 21
Continue learning Next.js step by step.
Middleware
Understand middleware and how it can intercept requests.
Theory
Middleware allows logic to run before a request is completed.
It can be used for tasks such as authentication checks, redirects and request processing.
Middleware can help control access to specific routes.
Example
import { NextResponse } from "next/server";
export function middleware() {
return NextResponse.next();
}Output
The request continues to the requested route.
Practice
Create middleware that checks access to a protected route.
Interview Questions
1
What is middleware?
2
Where can middleware be useful?
3
Can middleware perform redirects?
Quick Quiz
Which option best describes this chapter?
A.Request processing
B.CSS processing
C.Database storage
D.Image editing