Lesson Description

The "Simple Server Setup" Lesson is part of the full, Go & Vanilla JS: Fullstack Without Frameworks course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to set up the server using the HTTP package and the ListenAndServe function. He also shows how to handle different routes using the FileServer function, which serves files from a specified directory.

Preview
Close

Transcript from the "Simple Server Setup" Lesson

[00:00:00]
>> Maximiliano Firtman: After that, we have to create the server. And you can see we do have a public folder here we haven't seen yet what we have in the asset folder. You have a copy of the slide, so that's actually not part of the project, part of the training.

[00:00:13]
In the public folder, we are going to store all the static files that will be sent To the browser. In this case we are talking about the front end, okay? So far, we have just one web manifest that maybe we don't know what that is yet. We have CSS, okay?

[00:00:36]
We have some CSS code, not complicated, okay? We don't have even HTML and then we have a couple of images, like we have an icon, the logo in PNG and in SVG, and generic author, in case actor, generic actor. In case we don't have a picture for that actor, and kind of, that's all we have.

[00:00:56]
Okay, so the first step will be to just create a server and make that server serve that public directory, okay, so to do that in Go, it's actually pretty simple. If you are using Node js 99% of the time, you will be installing Express js like a framework to create.

[00:01:20]
It's a server, but go was optimized for this from the beginning, from its origins. So that's why you just go and create for example, let's say the address that we will be using, and we are going to say it's :8080 or 8000, this is the port number. You can also use local host, an IP address, a host, wherever you want to open.

[00:01:52]
So in this case, you decide the host that you want to open a port from and if you don't know the host, you have used colon and the port number. In this case, it's going to open that incoming connection from every available host in your computer or in the current computer.

[00:02:13]
So if you deploy that to a web server, it will use whatever host it has connected through DNS or the API P address or whatever on maybe later we will use directly 80. But for development purposes, we use 8080, in the future as a best practice, we may want to move that port number to the .m file, so then we can easily change that.

[00:02:39]
Or even have different .env environment files, one for development, another one for staging, another one for testing. So even on the same computer, we can be running more than one instance of the same server just by changing the port. Okay, but for now, for our first step, it's okay to create a constant like that, and then we can start trying to listen.

[00:03:02]
So we're going to use the http package. This is coming directly from Go. Listen and serve and we are going to pass that address. And then there is a second argument that is known as a handler that we are not going to use right now. We will talk about handlers later like this.

[00:03:20]
So, that will, will do something, okay? The problem is that this is probably returning an error. So we should be able to store that error and do something with that. Okay, so we can, receive that and then check if there is an error, and if there is an error, do something that.

[00:03:47]
If it's not nil, we wanna render a message or something okay, we can talk to the logger. We'll talk about logging later. For example, log.fatal says that you know what? This is not working at all, so it's a fatal error. We should stop executing the app right now.

[00:04:06]
Okay, it's not like any normal error, and you have fatal and fatal F. F is going to format the string so we can say something like the server has failed, and we use, then a placeholder for a value, and we pass that error as an argument. So this is pretty simple.

[00:04:30]
It's just opening a server and actually doing nothing, okay? So it's opening a server, it's listening for connections, but. Any connection that comes will actually respond in the same way. So to run the server, I will just use, go run. That's the current folder. Nothing happens, no message, nothing.

[00:04:57]
But I can go.
>> Maximiliano Firtman: To a browser, local host, 8080,
>> Maximiliano Firtman: And I'm seeing 404 patient phone, not matter the URL you are trying your server, you are getting your 404. Why is that? Because we don't have any handler. That's how we work in go with different URLs.

[00:05:25]
If I want to serve something, if I want to respond something on that URL, I need to add something to my server. But my server is up and running.
>> Speaker 2: Is there a package for the log? For some reason I'm getting [INAUDIBLE]
>> Maximiliano Firtman: So the log actually, [CROSSTALK] it's a package.

[00:05:44]
The same happens with HTTP, but I didn't type those. By the way, this is an import block, and actually VS code when I was writing log. Let me remove that from here. When you remove that and you're typing log, if you press return it's adding that importfolio. But if it's not happening, now you know what's the problem, okay?

[00:06:15]
But those packages are part of Go, so they are main packages. So you don't need to install. Anything, okay? Cool, so we need a handler ListenAndServe, it's a synchronous, cool. Which means the execution will stop there. Anything that you put after this, if the server is actually working and listening, It will be stopped there.

[00:06:42]
Of course, we can use concurrency and go with Go routines, but we don't need to do that. So before listening and serve, we can do that probably here, even before the address. We can create the handler. With go and again, this is kind of a review of Go.

[00:07:03]
We go, there are two ways to create handlers. You can use the second argument that I'm not using right now. In that case, you can create another value, and that value will have all the handlers. What are handlers? They are the waste Let's say objects for now, we don't have objects in Go, but that are actually listening for different routes.

[00:07:27]
Okay, if you are looking for that API, this is what you need to do. If you're using the Tada API, this is what you need to do. So we can use that argument, or we can be global and talk directly again to the http and we can say we wanna handle the rightly route with the function or with an option.

[00:07:48]
So we're going to handle then receive a pattern, for example, the route of the server. And we're going to say, you know what, for these, we will have a File server. The file server will actually take a file, one file, or it can be a whole directory, and it will serve that.

[00:08:11]
So this is kind of, I mean, that line will kind of emulate an Apache or any web server. So it will take a file system, A folder in your file system as and serve that. Okay, makes sense. Later, we'll talk about the architecture of our app. But let's say that in a real project, you can decide if you want go to be just for serving the API, like it's kind of a micro service Only for the API, or if you want Go to be everything.

[00:08:45]
It's the web server for the static files, and also the API handler, both. Right now, today we will be using Go for both. But have in mind that also you can have completely two separate projects. The front end and the back end and the back end doesn't know about the front end files.

[00:09:05]
They're not serving the front end files. And you will use another server mechanism for the front end can be just a static server, okay, and not served by go. But now we are serving everything by go or using go, so we're going to pass it direct. Directory and the director is public.

[00:09:23]
>> Speaker 2: Does that need to be a relative location, or?
>> Maximiliano Firtman: That's interesting. So should we, by default, is relative, which also might lead to some problem. For example, what if you run the project or the server from another folder. If I stop this, I go in here and I'm saying, go run Project/main.go.

[00:09:53]
So now, the current folder, I'm one folder up. So now, it's gonna be a problem because it's not gonna find. That public folder, okay? Yeah, you need to be very careful with that because by default it's relative. You can also get an absolute value if you want, okay?

[00:10:13]
Make sense?
>> Speaker 2: Could you always just use an absolute path instead.
>> Maximiliano Firtman: Yes, you can but have in mind that this is hard coded in your go project, and that will be go is compiled. So when you deploy that, this already hard coded, and if you are deploying that on the cloud, you probably don't know the absolute folder in the file in the build profile system of that cloud beforehand.

[00:10:41]
So typically, you have to ask for that dynamically most of the time, so we still have an error there. So if you try to see what's going on, so remember, you can go here in VS code and try to see the problems. It says that we cannot use File serve with handle, func.

[00:10:57]
Why is that? Because this is expecting a function and. Passing a value, not a function. So it's because, in this case, we use handle and not handle func. Func will receive a function. In this case, we pass a value that is a file server, just that. So if I want to try this now, I can open the terminal and run again with Go Run and see what happens if I refresh.

[00:11:25]
And now I'm seeing this. There is actually the list of files that I have right now. I can even get into the CSS file and I can see the CSS file. Why is that? Because I don't have an index or a default edge HTML file, I don't have a default document.

[00:11:46]
So just to see if this is working, so I want to create a very basic HTML inside public. So I will create a new file, index.html, and I will write the minimum possible value HTML file, by the way, you can use if you want. You use a clamation mark Tav if you're using VS code and thanks to a pattern or a framework known as EMET, is going to write a basic HTML for you, but I can even write it manually.

[00:12:19]
HTML with a title, hello from go, and maybe an edge one, hello from go. With that in mind, now I can go back to the browser, but I have a question for you, so I have just changed my HTML. Should I stop my server and rerun the server or not?

[00:12:44]
What do you think?
>> Speaker 2: No, because it just renders whatever is in there.
>> Maximiliano Firtman: Exactly, because it's rendering the file system. So it's actually I don't need to restart my server. I need to restart my server only when I change go files, okay. Okay, when I change the server, in this case, I'm changing the assets that the server is serving from the public directory, so I don't need to restart my server.

[00:13:11]
So I will refresh and I'm seeing now my index HTML. Okay, pretty basic, but now we have our web server for static files up and running. By the way, how do you restart the server for now, at least, because we will change that later. It's Control C, so you go to the terminal Control C, it just interrupts that process, and then you can run it again in case you are changing the server, okay?

[00:13:38]
But again, this is just a very basic web server that we have already set up.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now