NextRequest & NextResponse
The Next.js extensions to standard Web APIs.
Extending Web Standards
Instead of using Node.js's IncomingMessage or Express's Request objects, Next.js uses the modern Web API standards (the same ones used in the fetch API).
It then extends them with NextRequest and NextResponse to add powerful Next.js-specific helpers.
Quality of Life
Native Web Requests require you to manually parse raw cookie strings.
NextRequest gives you a convenient req.cookies.get('token') method.
It also includes req.nextUrl, which parses the URL for you, making it trivial to read query parameters or pathnames.
Usage in Route Handlers
You import them from next/server.
To read JSON bodies: const data = await req.json(); (Notice it is an async method).
To send JSON: return NextResponse.json({ success: true });
Express vs Next.js
Examine the difference between legacy Express.js syntax and modern Next.js (Web API) syntax when handling an API request.
Legacy Node.js (Express)
const body = req.body;
const token = req.cookies.token;
res.status(200).send({ body, token });
Modern Next.js (Web API)
const body = await req.json();
const token = req.cookies.get('token');
return NextResponse.json({ body, token });
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
NextRequestandNextResponseextend standard Web APIs.- Use
req.cookiesandreq.nextUrlfor convenient data extraction. - Do not use Express syntax like
req.bodyorres.send(). - Always
await req.json()to parse incoming JSON payloads.
Done with this concept?
Mark it complete to track your progress. No login needed.