Let’s also make an API for our application, to fully utilize Next.js’s capabilities.
First let’s create an /api/todo/
directory inside our app
folder.
Then create a file called route.js
. Like this:
my-app/
├── app/
│ └── api/
│ └── todo/
│ └── route.js
├── next.config.js
├── package.json
└── ...
Now here's how you define a route handler in a route.js
file:
// app/api/todo/route.ts
export const GET = (req) => {...}
Route handlers can handle a variety of HTTP methods, like: GET
, POST
, PUT
, PATCH
, DELETE
.
Next.js builds on top of the web native Request and Response and offers NextRequest
and NextResponse
.