From the course: Next.js: Creating and Hosting a Full-Stack Site
What are Next.js route handlers? - React.js Tutorial
From the course: Next.js: Creating and Hosting a Full-Stack Site
What are Next.js route handlers?
- [Instructor] All right. So at this point we've created most of the pages that we'll need for our application. So we're going to move on to creating some of the backend functionality for our app. Basically, we're going to create the part of our app that provides the data to the pages that we just created. So this is done in Next.js using something called Next.js Route Handlers. And let's take a look at what these route handlers are in the first place. So Next.js route handlers, in order to understand them a little bit better, let's think about how we've been creating other pieces of a Next.js application so far, right? For pages, all we had to do was put a file called page.tsx at whatever path we wanted that page to be accessible from. And as we saw, the same thing applied to things like layouts, right? We just had to define a file path and create a file called layout.tsx. And we also saw that this was true for not-found pages we could say, not-found.tsx, and put that in the path that we wanted that to apply to. So this all fits into what I said a little bit earlier about Next.js being a pretty opinionated framework about file names and file paths. And this sets us up for Next.js route handlers, which are defined in a very similar way, except the file name that we'll be using is route.tsx. So just like how when we defined a page inside a page.tsx file and exported a React component, in route handlers, what we're going to do is define a simple function with a name that indicates what method, what http method we want this route handler to respond to on that path. And we'll talk about that in much more detail shortly, but this is what route handlers are going to look like. So without further ado, let's jump in and see how we can start using these route handlers to create some backend functionality for our e-commerce application.
Contents
-
-
-
-
What are Next.js route handlers?2m 12s
-
How do Next.js route handlers work?7m 29s
-
(Locked)
Testing route handlers with Postman7m 33s
-
(Locked)
Creating a list endpoint for products5m 42s
-
(Locked)
Using route parameters in route handlers8m 10s
-
(Locked)
Creating a shopping cart endpoint9m 5s
-
(Locked)
Creating an add-to-cart endpoint9m 7s
-
(Locked)
Challenge: Creating a remove-from-cart endpoint1m 23s
-
(Locked)
Solution: Creating a remove-from-cart endpoint5m 53s
-
-
-
-
-