Hi everyone. In this video, we're going to be making a simple HTTP server in golang. So we talked about API's in the last video. So what APS are, they're essentially servers or back end services, which serve something to the clients. So the clients can request what request what they want to do to the server. And if the server has that procedure defined, it returns a status code along with some response.
So we're going to be doing the same in this video. So if you take a look at this code, so golang has it has a package for HTTP servers, it is called net slash HTTP. Now to create a simple server users do do HTTP dot listen and serve and your port address. So if you look at the definition of listen and serve, you can see that it takes two arguments. The first is the address and the second is a handler. So we're going to be talking about handlers soon.
Let's take a look at this address real quick. So our address can be our host address. or in our case localhost, because our server server is running locally. And then our port is going to be a special address or a special number, where all of the requests arrive. So if you take a look at this with this diagram, now we have a web application, we have a mobile application. They can be different functions defined, defined on the API.
So how would one know which API to hit? So API, which is essentially a back end service, can be listening on port 3000 can be listening on port 80, things like that. For example, if you're talking about mailing servers, then mailing servers listen on, sometimes they listen on port 25. And other ports like this. If you're talking about HTTP traffic, it's along Port 80. So these ports are defined so that it becomes easier for the client to recognize that address uniquely.
So let's see what happens when we run this server real quick. Dude on it's okay to go run, named or go It does nothing. So you might say that, okay, why it's not doing anything? Why is it? Is it? Isn't this printing anything?
Is there something wrong with that program? No. This was supposed to do this because what we are doing right now is we are listening for requests. So our API is ready, and it's listening for client requests. So then you would ask me, I'm good. How do I send something down to the API?
For that, my friends, the handlers there. So I talked about the second argument, which is handlers. And we can also define different handlers by talking about maxes. So mocks is a simple multiplexer. What it does is it takes the requests and routes it to the API. So let's see this in action.
So I can do HTTP dot, new serve mocks. And then I can possibly do this Now if we run this real quick, you're going to see that again, nothing happens. This is because while we have passed in a multiplexer, we haven't told the router or the multiplexer what to listen or what to serve. So we can say that okay, mux handle, give me a handler function which handles the slash route, and passes me something in return. So what this essentially means is whenever I go on localhost Port 3000, and whenever I hit the slash route, I get some data back. So let's see this in action.
So the mocks handler func takes in two arguments. The first is a response writer, and the second is the response reader. So HTTP dot response writer, and then HTTP dot response dot quiz feeder. And then we can simply say there. Okay. All right.
But I have a string of bytes. And in the bytes, right Hello, world? No, I'm gonna explain to you line by line again. Now let's see what happens. So to test an API, we can act as a client on our own by using the C URL or curl command. So what curl command does is it simulates a client and sends a request to the HTTP server for any server.
So I do a boot on me and nothing happens right now. So let's see what happens when I send a request down the server. Slash hello world. So I sent a request down to the slash route, it printed Hello, world. Now, taking a look at the code all over again. If I do a font dot print line request received then you can simply see what happens if I do a go run and do I if I do a curl on localhost 3000 is gonna say request received, then we can keep on doing it.
And it's, it's still listening for request. So this is what I meant when I said that it's listening for requests. So let's dive a bit deeper into our code. And let's try to understand each and everything again line by line. So first, we created a server which is listening on this port, and this host. So our host is our localhost because it's observing everything on local and then we passed in a multiplexer or a router.
So what a router does is router defines an endpoint that okay if you hit me on slash route, then these functions are going to be executed. So to execute these functions we require two arguments are response writer and a request reader. So, request, you might notice that request reader is a pointer argument over here. Because we, we require the reference to the request reader, but we can always create a response writer. So we get a request on the slash bot. And then we simply do we simply write hello world.
So if we change the route over here, again, we can do any route we want, we can say get going. And then it's gonna look it's gonna be listening on the same address, but a different route. Get going. Now, if you're using coal, and I recommend you to do so, then with a small life flag, you can see what request was sent. So you can see over here that we made made our HTTP server and it returned a 200 response which is open And then it gives you a lot of other data. Like what is the content length and the timestamp and everything.
So, in See you guys in the next video where we're going to be talking more about HTTP requests and what are the different types of requests that we can get. So thank you guys and let's see you guys in the next video.