Understanding callback execution in the event loop
Video: Understanding callback execution in the event loopWhen 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.
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Understanding callback execution in the event loop
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.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.