Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When a Node program starts, the event loop starts along with it automatically. In this video, we'll take a look at how the event loop works, and why it's important for your programs. Let's go to the Exercise Files. Go to chapter 2, video 3. I'm going to Copy server.js to the desktop. And now I'm going to take a look at it, in sublime text. Now, in contrast to many other environments, all input and output in Node is non blocking. So for instance, if you're making a call to a database, in many other environments, that call is going to block.
The environment is going to wait for all those results to come back from the database, stop your program, and then resume your program once those results are ready. Node however, keeps running the program. In this file, we're requiring the http module. We're going to create an http server that's going to keep on serving requests, regardless of how many requests come in. Then on line 3, we're defining a function called handleRequest. This function receives the request object and a response object, and then it can write things back to the response object.
So in this case it's writing back the http headers, and it's also writing back some content. Then on line 8 we're creating an http server. So we're using the create server method of the http module, and we're passing in the handleRequest function. Finally on line 10, we're calling the listen method of the server object. This listen method takes a port number as the first argument, and then an address as the second argument. So what happens, is Node starts up this server, and keeps listening.
The event loop keeps checking to see if this function is finished. And if not, it keeps the program going. Then every time a request comes in, this handleRequest function gets executed. Let's start up the server now, to have a look at how it works. I'm going to change directory to the desktop. So I'm going to type cd space, and then D for desktop, and then I'm going to press Return. Now I'm going to type node and server, and then press Return. So you'll notice that the program hasn't stopped. The event loop is keeping this program running. Let's go to the web browser.
I'm going to go localhost at port 3000. So now I get this message that says, Welcome to Node Essential Training, and our program is still running. I can Refresh it several times, and the program is still running. That's because the listen method is not fninshed. However, the handleRequest function gets executed several times. It gets exectuted once, for each time we load that request. The list method is never going to finish, it's just going to keep listening until we stop the server. So to do that, I'm going to press Ctrl C.
So in contrast to a program like this, I started out primarily developing web applications in PHP., All the code would run line by line, after finishing the last line of code, the PHP interpreter would stop, and the generated output would be sent to the browser. This process would happen for each and every http request reaching my web application. By contrast, when I build a web server in Node, that server keeps running until I stop it. One of the side effects, is that when I set a variable to a specific value, it will still be set the next time someone reaches the page.
The code keeps running and stays in memory. Node runs all of the code in a program at once, then waits for all callback functions to finish. It's possible to write a callback function that never finishes, which keeps a program running until it's forcibly stopped. With an understanding of how a Node works, we'll explore how to start a Node-based project in the next chapter.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98741 Viewers
61 Video lessons · 86072 Viewers
71 Video lessons · 69920 Viewers
56 Video lessons · 102187 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.