NextResponse
Next.js' extension of the Web Response object, with helpers for JSON responses, cookies, redirects and rewrites.
In more detail
NextResponse.json(data) builds a JSON response. In middleware, NextResponse.next() continues to the route, NextResponse.redirect() sends the browser elsewhere and NextResponse.rewrite() serves another path under the same URL.
Example
return NextResponse.json({ status: 'ok' }, { status: 200 });Related terms
- NextRequest — Next.js' extension of the Web Request object, adding a cookies helper and nextUrl, a parsed URL with Next.js routing details.
- middleware.ts — A single file at the project root that runs code before matching requests are routed, to redirect, rewrite, set headers or check sessions.
- HTTP redirect — A 3xx response telling the client that what it wants is at another address, given in the Location header.
- rewrite — Serving the content of a different path while the browser's address bar keeps the original URL.
Taught in
- Route Handlers: RESTful Endpoints with NextRequest — Next.js 15 & Modern App Router Architecture
- Edge Middleware: Auth Verification & Route Matchers — Next.js 15 & Modern App Router Architecture