In Next.js, you can create custom route handlers to serve data via RESTful endpoints. This example demonstrates how to set up a GET route handler to return a list of posts.
To create a route handler, organize your files within the src/app/api
directory:
src
└── app
└── api
└── posts
├── route.ts
└── posts.ts
First, define the data you want to serve in a separate file, posts.ts
:
Note
You would usually have data in something like a database, or in a markdown collection etc.
// src/app/api/posts/posts.ts
export const postData = [
{
id: 1,
content: 'This is the content of post 1',
author: 'John Doe',
},
{
id: 2,
content: 'This is the content of post 2',
author: 'Jane Smith',
},
{
id: 3,
content: 'This is the content of post 3',
author: 'Bob Johnson',
},
];
Next, create the route handler in route.ts
to serve the data:
// src/app/api/posts/route.ts
import { postData } from './posts';
export function GET() {
return new Response(JSON.stringify(postData), {
headers: { 'Content-Type': 'application/json' },
});
}
-
Data Definition: The
postData
array inposts.ts
contains the posts you want to serve. Each post has anid
,content
, andauthor
. -
GET Handler: The
GET
function inroute.ts
handles GET requests to the/api/posts
endpoint. It returns a JSON response containing thepostData
. -
Response Object: The
Response
object is used to send the data back to the client. TheContent-Type
header is set toapplication/json
to indicate that the response is JSON.
Once set up, you can access the endpoint by navigating to /api/posts
in your browser or using a tool like curl
, Thunder Client
or Postman
.
The endpoint will return the JSON data defined in postData
.
By using route handlers in Next.js, you can efficiently create and manage server-side logic, providing dynamic data to your application without needing a separate server setup.