Complete the code to import the middleware function from Next.js.
import { [1] } from 'next/server';
The middleware function is imported from 'next/server' as middleware.
Complete the code to define a middleware function that logs the request URL.
export function middleware(request) {
console.log(request.[1]);
return new Response('OK');
}The request.url property contains the full URL of the incoming request.
Fix the error in the middleware to correctly forward the request to the next handler.
export function middleware(request) {
return [1](request);
}In Next.js middleware, to forward the request, you use fetch(request) to continue the chain.
Complete the code to create a matcher that applies middleware only to API routes starting with '/api/'.
export const config = {
matcher: [1]
};The matcher '/api/:path*' applies to all routes starting with /api/ including subpaths.
Fill all three blanks to create middleware that blocks requests without a valid token header.
export function middleware(request) {
const token = request.headers.get('[1]');
if (!token || token !== '[2]') {
return new Response('Unauthorized', { status: [3] });
}
return fetch(request);
}The middleware checks the X-Auth-Token header for the value Bearer and returns a 401 status if unauthorized.