route.js
API reference for the route.js special file.
Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs.
Reference
HTTP Methods
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
.
Parameters
request
(optional)
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
(optional)
params
: a promise that resolves to an object containing the dynamic route parameters for the current route.
Example | URL | params |
---|---|---|
app/dashboard/[team]/route.js | /dashboard/1 | Promise<{ team: '1' }> |
app/shop/[tag]/[item]/route.js | /shop/1/2 | Promise<{ tag: '1', item: '2' }> |
app/blog/[...slug]/route.js | /blog/1/2 | Promise<{ slug: ['1', '2'] }> |
Examples
Handling cookies
Version History
Version | Changes |
---|---|
v15.0.0-RC | context.params is now a promise. A codemod is available |
v15.0.0-RC | The default caching for GET handlers was changed from static to dynamic |
v13.2.0 | Route Handlers are introduced. |