Source: Link
Introduction
Next.js is a React framework that allows you to create single-page Javascript apps. This framework has various advantages for our clients' applications and our development team.
As users, we get increasingly irritated as our expectations are not met by websites and apps that take longer than milliseconds to load. We've started utilizing Next.js for various reasons, the majority of which are linked to speed and performance.
Technology decisions play a huge part in producing highly performant, scalable, and successful apps, and as such, we've started using them for a variety of reasons.
We'll learn about the API routes in next.js. We will also learn the concept of Dynamic API routes and API structure in Next.js. We will also see how to send a response in JSON and HTTP formats.
Let us dive deep into the concepts of environment variables in Next.js.
API Routes in Next.js
API routes provide us with a solution to build your API with Next.js.
Next.js API routes adhere to the REST (representational state transfer) protocol, a standardized protocol used by the majority of internet APIs. As such, we have a great amount of flexibility for designing the routes of the API.
This API will support two routes: one that accepts a slug and another that accepts a JSON object containing a query attribute.
- Route of the slug: A slug is a unique identifier for a URL in web development, often found at the end of the URL. The slug in the URL https://example.com/blog/this-is-the-post is this-is-the-post, for example. The URL for this API will be something like this: http://localhost:3000/api/[slug], where [slug] is the desired query. It's termed a dynamic route since the URL of this route changes depending on the request. On sending a GET request directed to the server with the desired query URL will complete this API route.
- This API route accepts a JSON object with the type "query": QUERY, where QUERY is the requested format. This API route cannot be accessed simply by changing the URL; it must be accessed using a POST request directed to the server, with the body of the request including the object in the format indicated.
All of these routes will be defined as lambda functions, with a req request argument (for interacting with the user's request) and a res response parameter (for interacting with the response data to send back to the user).
Any file in the pages/api subdirectory is mapped to /api/* and regarded as an API endpoint rather than a page. They're exclusively server-side bundles; therefore, they won't add to the size of your client-side bundle.
The following API route is used pages/api/user.js, for example, delivers a JSON response with a 200 success code:
export default function handler(req, res) {
res.status(200).json({ name: 'Coding Ninjas' })
}
To make an API route work, you must first export a default function (also known as a request handler), which will then receive the following parameters:
- req: http.IncomingMessage instance, as well as various pre-built middlewares
- res: A http.ServerResponse instance with several utility functionalities
You may use req.method in your request handler to handle different HTTP methods in an API route, like this:
export default function handler(req, res) {
if (req.method === 'POST') {
// Process a POST request
} else {
// Handle any other HTTP method
}
}
Implementation Details
API Routes can be used to create a whole API for new projects. If you already have an API, you don't need to use an API Route to route calls to it. Other applications using API Routes include:
- Masking an external service's URL (for example, using /api/secret instead than https://company.com/secret-url)
- To safely access external services, use Environment Variables on the server.