0
0
NextJSframework~5 mins

Route handlers (route.ts) in NextJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of a route.ts file in Next.js?
A route.ts file defines server-side route handlers that respond to HTTP requests like GET, POST, etc., for a specific API route in Next.js.
Click to reveal answer
beginner
How do you export a GET handler in route.ts?
You export an async function named GET that takes a Request object and returns a Response object.
Click to reveal answer
intermediate
Why should route handlers in Next.js be asynchronous?
Because they often perform tasks like fetching data or writing to a database, which are asynchronous operations. Using async lets the server wait for these tasks without blocking.
Click to reveal answer
beginner
What is the difference between a route handler and a React component in Next.js?
A route handler in route.ts runs on the server to handle HTTP requests, while a React component runs on the client or server to render UI.
Click to reveal answer
beginner
How can you send JSON data from a route handler?
Return a Response with JSON.stringify data and set the Content-Type header to application/json.
Click to reveal answer
Which function name is used to handle POST requests in route.ts?
APOST
BgetPost
ChandlePost
DpostHandler
What should a route handler return in Next.js?
AA React component
BA Response object
CA JSON object directly
DAn HTML string
Where do route handler files like route.ts live in a Next.js project?
AIn the <code>components</code> folder
BIn the <code>pages/api</code> folder
CAnywhere in the project
DIn the <code>app</code> directory under the route folder
How do you access the request body in a POST route handler?
AUsing <code>await request.json()</code>
BUsing <code>request.body</code> directly
CUsing <code>request.params</code>
DUsing <code>request.query</code>
Which HTTP methods can you handle in route.ts files?
AOnly GET
BOnly GET and POST
CGET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD
DOnly POST and PUT
Explain how to create a simple GET route handler in Next.js using route.ts.
Think about how to respond to a GET request with JSON.
You got /4 concepts.
    Describe the difference between client-side React components and server-side route handlers in Next.js.
    Consider what each part does and where it runs.
    You got /4 concepts.