We're going to learn how to read dynamic segments from a URL using dynamic routes.
Duration
2 min
Access
Pro
Transcript
Available
We're going to learn how to read dynamic segments from a URL using dynamic routes.
Video Transcript
Instructor: Let's create a folder called dynamic.
Now, let's create a file called openAngleBracketSlugWeCloseTheAngleBracket.js. Let's create a function called handler() that receives a request and a response. What we're going to do is, we are going to extract this log from the request.query. Let's return it as JSON. The last thing that we have to do is to export handler as a default export.
Now, we're going to call the API/API/dynamic and let's add 1 here. As we can see, we are returning the 1 in the URL in the JSON response. Let's change 1 with 2 and we'll see that we are returning 2 as a slug. It's worth noting that if we try to visit dynamic/1/2, we get a 404 back, because that route doesn't exist.
To recap, in order to create dynamic API routes, we have to create files that start with an angle bracket, then we can add the dynamic segment we want, and then we close the angle bracket. After that, we can get the dynamic segment from the request.query.
Duration
2 min
Access
Pro
Transcript
Available
Course lesson
Create a Dynamic Next.js API route
We're going to learn how to read dynamic segments from a URL using dynamic routes.
Access required
This lesson is available with an active egghead membership.