
Lesson Description
The "Creating Another Route Handler" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano walks through creating a `writeJSONResponse` function to handle the common code for setting the content type, encoding data into JSON, and handling errors. He demonstrates how to define this function and use it in the `getTopMovies` and `getRandomMovies` handlers.
Transcript from the "Creating Another Route Handler" Lesson
[00:00:00]
>> Maximiliano Firtman: Of course, the next step will be to replace the dummy data with real data. But before doing that, let's create another api/movies/random that right now is 404. Why random because, I mean, in our homepage, maybe later, we wanna show random movies just recommend random movies from the database.
[00:00:29]
So just to review the process, to create a new handler, we need to go now to handlers >movie_handler, and we do have GetTopMovies. So I need another one, I will copy and paste, I know that copy and pasting code is not a good idea, but for this particular reason, I will copy and paste and get random movies, okay?
[00:00:59]
Let's just change the dummy data, we will remove the dummy data in a couple of minutes. And here you can see we have a moment like setting the content type, checking encoding the data into JSON, verifying the error. We need to do this on every web service, right?
[00:01:22]
So every call, every web service call, will need that, so maybe it's a good idea, instead of copy and pasting and duplicating the same code, just create a function for that, so that's pretty common. If you're using a framework that we are not, okay? But if you're using a Go framework, such as the Gin web framework, they will do that for you, okay?
[00:01:43]
But in this case, we are not using a framework we can create our own micro library that will do that for you. So we can create a function that will just have this part, that's the idea, and then we call that on every API, on every handler for an API.
[00:02:01]
So, anywhere in this file, we can create a function that, is gonna be a method. So again, remember, every time you create a method in parentheses before the signature, or actually part of the signature as a prefix. In parentheses, we put the name of the structure where this method belongs, and I'm going to call this writeJSONResponse and if you pay attention, it's lowercase w.
[00:02:37]
That's because I don't want to make this available from the outside of this package, okay? It's a private method, so it's gonna have the same, or when you shot the writer, we don't need actually the request. So we're going to have the w as a response writer, because we are going to write there the JSON, and we have the data, okay?
[00:03:05]
But the thing is that we want this function to be compatible with any kind of data that we want to respond and in Go we don't have the any type or object or something like that. So I'm not sure if you remember how to define asset type anything, does anyone remember how to do that in Go?
[00:03:29]
Interface. Interface, and code block which means any value that has that interface that has no rules applies. So because he has no rules, he has no protocol, no list of method, any value will fit there. So this is kind of any as in other languages, okay? We receive the data and actually what we need to do is this part, so I will copy this and maybe I need to make some changes for example, it's not movies anymore, it's actually data.
[00:04:07]
Let me close the folder view so I need to send the data, need to encode the data. And then we can
>> Maximiliano Firtman: Finish this later with login, the error, okay? But now then on GetTopMovies instead of doing this, I just need to call that bright JSON response, okay?
[00:04:35]
That makes sense. Remember how to call a method of your own structure. I know that your brain, if you're coming from JavaScript or other languages, think about this but we don't have this here. We use the name of this special argument we receive here, so in this case, is H.writeJSONResponse.
[00:04:57]
We pass the response writer and the data that is movies, and we do the same on get random movies.
>> Maximiliano Firtman: So we will use that a lot, so every API, we need to respond with a JSON response so let's focus that code only in one section. Now, I created the handler but I didn't set to HTTP, hey, you have another handle.
[00:05:28]
So in main.go, I have api/movie/stop, and I need to create another one, another HandleFunc that says api/movies/random. That will call movieHandler, GetRandomMovies. Remember, be careful, don't add parentheses. We are passing the function as an argument; we are not executing the function, I have a missing C here, okay?
[00:06:00]
That so I have an error here, because it's H, that's why it's not working. And now, do I need to restart the server? No, it should have already happened. So I go to the browser, I refresh, and I'm still not getting it, why? Because I didn't save. So remember that you need to save every time you wanna get this?
[00:06:30]
>> Student: Does that trailing slash matter to random.
>> Maximiliano Firtman: Yeah, so also in this case, we added this trailing slash just to test this. It's important to decide if you want or not the trailing slash, it's not the same. Also, it's important the order in when we are registering the handlers, why I'm saying this?
[00:06:54]
For example, what if we first serve the static file and then we add these handlers.
>> Maximiliano Firtman: Will it work or not? The order here matters because we are registering handlers in order and in this case we are actually taking the whole folder so maybe API will also fit into that URL.
[00:07:24]
And also the file server sometimes it's when the trailing slash is tricky, because if you're trying, for example, forward /api and it's getting into the file server. And the file server realizes or let's try with images, because we do have an images folder in the public folder. Because there is a folder, if I try the URL/images, the file server will respond with a redirect so images trailing slash.
[00:08:04]
>> Maximiliano Firtman: And that will do a lot of trick situations in your app, so be careful in this case, I don't think we need the trailing slash for the API, URLs, it's up to you, if you like them, it's okay. And I will just put the order back as we did before so static files at the end, the last fallback.
[00:08:30]
If no API, if no handler actually took responsibility of that URL, let's go to the file system that's kind of the idea. The same happens on other servers, I mean, if you have experience with Express.js, for example, it's pretty similar in terms of the order where you register your routes.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops