route.js
路由处理程序允许你使用 Web 请求 和 响应 API 为给定路由创建自定义请求处理程序。
¥Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs.
HTTP 方法
¥HTTP Methods
路由文件允许你为给定路由创建自定义请求处理程序。支持以下 HTTP 方法:GET
、POST
、PUT
、PATCH
、DELETE
、HEAD
和 OPTIONS
。
¥A route file allows you to create custom request handlers for a given route. The following HTTP methods are supported: GET
, POST
, PUT
, PATCH
, DELETE
, HEAD
, and OPTIONS
.
export async function GET(request: Request) {}
export async function HEAD(request: Request) {}
export async function POST(request: Request) {}
export async function PUT(request: Request) {}
export async function DELETE(request: Request) {}
export async function PATCH(request: Request) {}
// If `OPTIONS` is not defined, Next.js will automatically implement `OPTIONS` and set the appropriate Response `Allow` header depending on the other methods defined in the route handler.
export async function OPTIONS(request: Request) {}
export async function GET(request) {}
export async function HEAD(request) {}
export async function POST(request) {}
export async function PUT(request) {}
export async function DELETE(request) {}
export async function PATCH(request) {}
// If `OPTIONS` is not defined, Next.js will automatically implement `OPTIONS` and set the appropriate Response `Allow` header depending on the other methods defined in the route handler.
export async function OPTIONS(request) {}
很高兴知道:路由处理程序仅在 App Router 内可用。你不需要同时使用 API 路由 (
pages
) 和路由处理程序 (app
),因为路由处理程序应该能够处理所有用例。¥Good to know: Route Handlers are only available inside the App Router. You do not need to use API Routes (
pages
) and Route Handlers (app
) together, as Route Handlers should be able to handle all use cases.
参数
¥Parameters
request
(可选)
¥request
(optional)
request
对象是 NextRequest 对象,它是 Web 请求 API 的扩展。NextRequest
使你可以进一步控制传入请求,包括轻松访问 cookies
和扩展的、已解析的 URL 对象 nextUrl
。
¥The request
object is a NextRequest object, which is an extension of the Web Request API. NextRequest
gives you further control over the incoming request, including easily accessing cookies
and an extended, parsed, URL object nextUrl
.
context
(可选)
¥context
(optional)
type Params = {
team: string
}
export async function GET(request: Request, context: { params: Params }) {
const team = context.params.team // '1'
}
// Define params type according to your route parameters (see table below)
export async function GET(request, context: { params }) {
const team = context.params.team // '1'
}
目前,context
的唯一值是 params
,它是包含当前路由的 动态路由参数 的对象。
¥Currently, the only value of context
is params
, which is an object containing the dynamic route parameters for the current route.
示例 | URL | params |
---|---|---|
app/dashboard/[team]/route.js | /dashboard/1 | { team: '1' } |
app/shop/[tag]/[item]/route.js | /shop/1/2 | { tag: '1', item: '2' } |
app/blog/[...slug]/route.js | /blog/1/2 | { slug: ['1', '2'] } |
NextResponse
路由处理程序可以通过返回 NextResponse
对象来扩展 Web 响应 API。这使你可以轻松设置 cookie、标头、重定向和重写。查看 API 参考。
¥Route Handlers can extend the Web Response API by returning a NextResponse
object. This allows you to easily set cookies, headers, redirect, and rewrite. View the API reference.
版本历史
¥Version History
版本 | 变化 |
---|---|
v15.0.0 | GET 处理程序的默认缓存已从静态更改为动态 |
v13.2.0 | 引入路由处理程序。 |