Check out a free preview of the full The Hard Parts of Servers & Node.js course
The "Local Node Development" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:
Will describes how to run JavaScript code and Node apps locally for the purposes of testing, using the localhost domain.
Transcript from the "Local Node Development" Lesson
[00:00:00]
>> Will Sentance: The problem is though, but if ever I wanna test, is my code working right, do I need to load up to Amazon's computer? Wait for it to load up, okay, we're good to go and open the website now. Michael, do you mind just opening twitter.com and checking my website is working?
[00:00:15]
Did I get the right request? I handled it, no, I sent back the wrong one. Hold on, Michael, can you just stay there and open it again in three minutes? Now, we definitely don't have to do that. So we want to do it all on our computer. Both the being Michael sending a message to the computer that's got the open connection to the Internet ready to listen for your messages, running node.
[00:00:40]
Running JavaScript to listen to the message, look at it, send back the right data. We wanna do that on the same computer in our dream world as actually loading the website to look at that, to send a message to that computer.
>> Will Sentance: Let's see if that's possible, have a look at this.
[00:00:57]
So I'm on my computer, they call this my development machine. It's my computer which I'm developing. Here's what I do.
>> Will Sentance: I load up,
>> Will Sentance: Node, it turns on the JavaScript engine. It turns on the node C++ features. And it turns on the computer's internals.
>> Will Sentance: Internals, there it is, okay.
[00:01:28]
>> Will Sentance: I set up the open channel to the Internet, right, the open channel to the Internet at port, well open channel to the Internet socket. Now in theory, if I wanted to run and test whether I was waiting messages correctly and ready to send them back correctly I'd need a totally separate, Michael you need to send a message to my computer, right, to see if I get a message in and send something back.
[00:01:55]
That would be ridiculous. I'd have to do that. I'd have to sit calling Michael to send messages to my computer the whole time, to see if something is coming in.
>> Will Sentance: So the designers of operating systems realized this was ridiculous. So they invented a feature, I'm sorry. They invented a feature that allows us to open our web browser on the same computer we're running, our node app, open our web browser.
[00:02:24]
Open not through the .com, but a special domain name called local host.
>> Will Sentance: And then put, I don't know, tweet three or whatever. And it's gonna send out a message, not to the Internet. But it's a special domain name that loops straight back in to our own computer where we were listening.
[00:02:53]
It loops it straight back in. By the way, local host has a domain especially an IP address 127.0.0.1. And this allows us people to write code to set up our server to open the socket on our computer. But to have the ability to access our own computer by not having to go to another computer, open twitter.com that sends a message to our computer.
[00:03:22]
But do it from our own computer with a special built into our operating system domain name, local host that loops us right back into our own computer. That sends the sends the outbound HTTP message not out into the Internet, but straight back to our own computer. And we have a special IP address that references from inside our own computer.
[00:03:43]
References our own computer, it's 127.0.0.1, which has a domain name so you don't have to remember it, local host. Here's a problem, we tend not to want to use the port 80, that's the entry point 80 that's used default for the browser going out. Instead, we want to use any one of the numbers of the 64,000 entry points we can use.
[00:04:04]
And people tend to use set from JavaScript 3000 silly ones like this. I don't know why they do this and then we set the port up here to be listening on port 3000. We have 64,000 options, right? And but that means what port does the browser default to sending out from Michael?
[00:04:22]
>> Michael: 80.
>> Will Sentance: 80 but if we're not gonna be entering at that point into our own computer we better declare when we send out with the help of a colon the exact port that we're gonna send out that we expect it to come in at. And we put it in there.
[00:04:37]
And that means, we can send out a little HTTP message, which never really even leaves our computer. By the way, in the old days, before this was built into your operating system, they used to literally take a wire out the back of the computer, and loop it back into the machine.
[00:04:52]
But now it's part of the operating system so the message doesn't even leave the computer. It comes straight back in and enters. And that's gonna allow you people to both write your code for your server to listen and receive messages and send stuff back. But also test it's working by opening the domain name that accesses your own computer directly.
[00:05:10]
Cuz remember, what is a computer that's a server? All it is is an open channel through the computer's networking, see I do not write from this side, networking internal feature that you set up from JavaScript via node. That's all it is. That's all it is computed with an open channel with those three lines instead of the open channel, the open network, the open socket.
[00:05:34]
.And set the right port. And then we get to access our own computer's open channel using that domain name. And that's what we're gonna do in the pair programming. Okay, I think it's what I said here. What about testing our server? Do you need to load the code to be run on AWS to test it live?
[00:05:52]
No, operating system developers, that's the people who built this whole computer, included the loop back feature, with local host as a pseudo domain. A pretend domain name that sends out your message not out to the Internet, but straight back into your own computer. This is what we'll be doing in the pair programming.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops