Check out a free preview of the full Introduction to Serverless Functions course
The "Return Data by ID" Lesson is part of the full, Introduction to Serverless Functions course featured in this preview video. Here's what you'd learn in this lesson:
Jason continues live coding the application, and adds a feature to the app to allow users to filter the movies from the JSON file by ID by building a new serverless function that queries movies by ID.
Transcript from the "Return Data by ID" Lesson
[00:00:00]
>> What we just did is to set up a pretty basic echo, where we pass in a query parameter and then bring that back. To make this more practical, what we're gonna do next is we're going to allow someone to filter the movies by an id. So to do that, we're going to create a new function, and we're gonna call this function movie-by-id.js.
[00:00:26]
And inside of it, I'm gonna pull in our movies again. So we'll require that from data movies, then we're gonna export our handler. And as usual, that's going to be async. And because I know I need the queryStringParameters, and because I don't necessarily want to have to deal with anything else in the event, I'm just gonna destructure right in, The argument like that.
[00:00:56]
So then, inside my queryStringParameters, I need the id. So I'm gonna pass in an id parameter. And we'll get that out of queryStringParameters, right? And then I wanna get my movie. And I'm gonna do that by running movies.find, and we'll just do a single movie and make sure that the id equals the id, okay?
[00:01:25]
So from here, this is the one movie that matches the id that came in. So there's a possibility that we're not gonna get an id, or that the id is gonna be for a movie that doesn't exist, so let's check for that. If we don't have a movie, I'm going to instead return a statusCode of 404, which means that we didn't find one, and the body that we're gonna send back is Not Found.
[00:01:54]
The body is kind of optional in a 404 case, because the statusCode is what's gonna trigger things. But it never hurts to be clear. But if we get past this point, so if we pass to this gate, then down here, we know we can return the movie. So let's return a statusCode of 200, and a body of JSON.stringify, our movie.
[00:02:19]
Okay. Then I'm gonna start the server again, netlify dev. That's what I did wrong. Okay, and I spelled that wrong again. Boy, this is queryStringParameters, got it. Let's roll and try this thing out. So I'm gonna head into my movies.json and I'm just gonna grab one of these id's, and let's give it a try.
[00:02:51]
So I'm gonna go movie-by-id, and then as a query string I'm gonna pass in one of the movies. Okay, good. So we got Batman versus Booperman, that was the one that we wanted. So let's try another one. Starship Boopers, perfectly. And let's try one that doesn't exist. We'll just send in no, and we get Not Found.
[00:03:14]
Let's try it with no id. We get Not Found, perfect. So we're getting the behavior that we wanted, if we send in a valid id, we get the one movie that matches our id. If we don't send in a valid id or we don't send an id at all, we get a 404, that's the behavior that we're after.
[00:03:34]
And again, just to kind of reiterate here, we wrote 18 lines of code, that's a full API end point, we just set up a look up for a movie based on its id. And we didn't have to think very much about servers, we just got to think about how the logic works.
[00:03:52]
We have our movies, we get the movie id that we want, and then we just filter the array and return that. We're only writing logic, we're not writing boilerplate. And I really, really like that about serverless functions. It's one of the things that I think is so powerful about it.
[00:04:07]
A boilerplate is limited to one line, we have to export a handler and we got to make sure that we return a statusCode and body. That's very little boilerplate to do a whole lot of cool stuff.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops