Check out a free preview of the full Go for JavaScript Developers course:
The "Web Servers & Routes" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna begins building a todo app using Go. Through a code-along exercise, students are shown how to spin up a basic web server and handle routes.

Get Unlimited Access Now

Transcript from the "Web Servers & Routes" Lesson

[00:00:00]
>> And we're gonna look at spinning up a real small web server, and look at what go looks like when interacting with the browser a little bit, and a super brief look at working with a template, an HTML template and go. So kinda this melding of front end back end for just a few minutes.

[00:00:16] So we're talking about routes, route handles, go handles routes using the net/http library. And it's gonna look something like this, where you define in your main function, an entry point for where your route is going to go. So if you call http, and then the built in method HandleFunc, it's gonna take two arguments.

[00:00:38] The path you want to intercept from the browser, and then what you wanna do when that path is recognized. So, for this example, we're saying that, hey, when the user navigates to the homepage, go fire the function home, which we don't have to find here, and then execute whatever code lives in that route handler.

[00:00:57] So, right now in my file, the only thing I'm doing is printing out the words building a web server. I'm gonna move up my terminal, and then get my go run function ready to go. My intersection 11_server/code/server.go. If I run it now you see the words building a web server.

[00:01:13] But let's implement a quick example of a route handler. So on our input statements, let's add a library to handle some routes. So net/http, so parentheses. And then in main I'm gonna kill my print line for now. And have my first handle function be http.HandleFunc. Let's just do the home path.

[00:01:45] And then it's gonna call a home function which we'll write next. So up here I'm gonna say func home, and then for now let's just print line, Home. So see I'm already getting an error, which is intentional. If I run this, it's gonna say, that this type, this home type, it's a function but it's expecting a bunch of arguments we're not passing in.

[00:02:15] So, let's grab the docs real quick. I'm gonna say go doc http.HandleFunc. With this guy up so you can see what's happening here. So here's some information about the HandleFunc. So, it's gonna take a string, as its first argument, and then a route handler, the handle function home.

[00:02:36] And then that function is expecting two different things, a writer and a request pointer. And so we need to modify our home function to take these two arguments. And these are gonna be arguments that we passed to any route handler that we build throughout this web server or in the next exercise as well.

[00:02:54] So I might start copy pasting this at some point. But for now, let's talk about what's happening. So the first argument is the writer. Where are we writing anything that happens too? So it's gonna be http.ResponseWriter. And this is a built-in type that comes from the http library.

[00:03:10] And the second argument is the request. A variable that represents the request that was passed through. So, we're gonna call this http.Request, which is a pointer and that also comes from the http library. So these are built in go types and required arguments to this route handle function home.

[00:03:27] So if we run this now, the go doc1, this guy, cool. So, the next thing we wanna do is that, instead of just handling this function, we also wanna spin up a server and tell our application here where we want to serve up any assets that are gonna show up in our browser.

[00:03:45] So we're gonna add a couple lines to our main function. So after we have our route handlers, the next thing I wanna do is for my own benefit, because I always forget, I'm just gonna say the port like server is running on port. Let's work with 8080. And this is just a note to myself, so I know what port I'm gonna navigate to in my browser using localhost.

[00:04:06] And then I'm gonna use log.Fatal. And we'll talk about what this means in just a second. But within log.Fatal, I am passing it http, and then the method listen and serve, which takes two things, the port we're looking for in the browser. And then a second argument which is optional, so we'll pass in nil.

[00:04:26] Print line has a T in it. Cool. So our http library is kinda waiting to intercept our /home route landing page route. And at this point, our http.ListenAndServe is what's actually spinning up our server. So our servers being spun up here, we're looking for, if we ever hit this port in our browser on localhost, and when that happens, is there a route handler for the path we just hit?

[00:04:53] And then log.Fatal is going to expect an error, and if there's an error, do something with it. So, assuming this goes great, log.Fatal won't do anything. If something bad happens, log.Fatal will log that error for us. And what's interesting and kind of something that took me a minute to wrap my head around too is that, the stuff that's in this parenthesis here, that will get executed, regardless.

[00:05:14] So even though we're wrapping this in the log.Fatal, we're still actually executing the method ListenAndServe, we are still spinning at that server. The log.Fatal is just kind of a back up wrapper to be like if something goes wrong, go handle it this particular way. Cool, so now we run server.go, you'll see we have our server is running on port 8080.

[00:05:36] I'm gonna navigate over to my browser. Wow, localhost, 8080. So see, nothing's happening on the screen here. But if I circle back to my browser, you'll see that I've now print line home in my actual terminal. So this home function doesn't actually get triggered until our server intercepts this particular route, the forward slash.

[00:06:05] Because we've spun up our server and it's listening and ready for action. So if you think about the comparison between reactor router versus doing something like this, it takes very little code to actually spin up a server and get it rolling using go, which I find interesting.