This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.
Transcript from the "Server Solution" Lesson
[00:00:00]
>> Jem Young: Okay, everybody's looking confident, that means you're probably done. Or you're wrong and you have a typo and you don't know it yet. We'll find out real fast. So let's go ahead and run that. So if we just run node simpleServer.js, which I already did here, node simpleServer.js
[00:00:16]
>> Jem Young: And in your web browser, just point yourself to your local host on port 8080.
>> Jem Young: You should see something that looks like this, let me blow that up a bit. My bookmarks are showing. You
>> Jem Young: Nothing to it. I shouldn't say that, when it comes to engineering I try not to say simple, or it's easy, because that's totally relative.
[00:00:50] And then if you don't get it makes you feel bad. But this is the simplest server I could think of. There's probably someone out there who can write one assembly, well it's not technically simpler, probably slightly faster, it doesn't matter. But the point is anything can be a server.
[00:01:05] And we can script a server in every language, every web language there's a way to write a server. Let's go back and see what are we doing here. What we're doing is we're in node, we're importing the http library. And then http has this ability to create a server, and we're just gonna write to the output, to the response, we're gonna writes Hello World.
[00:01:28] And we're listening on port 8080. And we're setting a console just to let you know that the server is up and running once we get it going. The basics of every single node server you're ever gonna use is this call back with request response. It may be, you might use promises, you might use async await, but there's always gonna be request response.
[00:01:47] And the third parameter is usually next, which is used for creating middleware when you're saying do this request and then pass it through these middleware and then respond. But in this case, we're not using any of that, but be familiar with this pattern. I probably won't get too much into it, there are other Frontend Masters' courses which go much, mush deeper into note servers, much more granular level, and we'll think those out later.
[00:02:10] But in general it's always going to be request response, that's the basic pattern to every single nodes server you'll ever write. And we're listening on an arbitrary port, it could be port, I don't know 6304 or even 6305, it doesn't matter. Generally ports below want to say 1000 are reserved, so the internet runs over Port 80, on HTTPS, the Internet runs over port, anybody?
[00:02:44]
>> Speaker 2: 443
>> Jem Young: Yes, 443, yes, very good. There's this concept of reserved ports and pretty much anything below 1000 is probably reserved by some program or language. So you don't want to mess with those, but choosing an arbitrarily high number like port 8080 is usually pretty safe. And we're in localhost, I can also type, that's just shorthand for 127.0.0.1.
[00:03:07] Whoops, am I running Nginx, [LAUGH] I am running Nginx. Usually 127.0.0.1 is a loopback command. It's just saying instead of going out to the Internet and hitting a server on port 8080, I'm just gonna hit my localhost running important at 8080. I have Nginx running from a different configuration.
[00:03:28] Don't worry about that you didn't see anything, just pretend that didn't happen. But generally, when you're running, doing local development, you're always gonna hit your localhost. If you do remote development, you can hit remote host, and that's a server. Or if you're wondering how I did the line numbers, I put it in the notes here, but in them it's good to try that.
[00:03:49] And I'm just gonna kill this command. See if you wanna show the line number oops, I hit Esc, last line node, and I'll say set number. I think that's right, yes, thanks to my line number.
>> Jem Young: And if I want to hide them, I think say set number again.
[00:04:10] Oops, nope, no number. I like that, it's simple, to the point. Set no number and [INAUDIBLE], just in case. I'm not trying to do anything that you're like, well, how did he do that? I want you to know everything I know, except my in depth knowledge of Fast and Furious trivia, that's mine.
[00:04:28] No one else can have that.
>> Jem Young: And right clear.
>> Jem Young: That server, let's the simplest, bare bones way of creating an application. Or, like Sam said, something that responds to requests. Pretty straightforward, we can handle this. And now we're going to get much more advanced this is this is as simple as it gets.
[00:04:54] And now we're going to pick up a little bit of speed. We're talking about servers at scale, because obviously I can't run frontendmasters.com on my laptop. I probably could, actually you'd be surprised that how efficient servers are now, but it's really not a good idea. It gets more complicated, which we'll talk about the complications, that's why we're here.
[00:05:13] If servers were this simple, everybody would be full sync engineer. [LAUGH] But they start off really, really simple, as far as these servers go.