Loading
Lesson 24
Courses / Build a Web App like Twitter from Scratch Using HTML, CSS, JavaScript, Express, MySQL
Handling SIGINT and SIGTERM in Express to Properly Close the Database and Server

Video Transcript

Welcome to another lesson. In this lesson, we are going to learn how to handle an interrupt and a term signal for our Express server so that we can properly close the database connection. So let's get started. Here I have the file server.js. In server.js line 71 through 73 have a call to the listen method. Now we have in line 77 connection.in but that's not in its proper place because by the time I hit line 71 it starts the server but then it continues on executing the code and it automatically and it goes on to close the connection right away right after the server has been started due to the asynchronous nature of JavaScript. That's not what we want. We only want to close that connection when the server is shut down. How can we do that? One way is by handling the signals for interrupt if I press ctrl c and for term for termination. And once we get those signals we can proceed to close the server right after closing the connection to the MySQL database. Let's get started. So after you make the call to listen you can do the following. It's call on the process object and you can call process on and then you have to say the name of the signal that you want to do something when that is triggered. For example, sig int would be when I press ctrl c an interrupt signal. That's what I usually do to kill my server. So let's try that out. The second argument would be the callback function you want to execute right after that signal was emitted. Let's put a console log here. Sig int emitted. Let's see what happens. I'm going to save this. I'm going back to my terminal window where I have my server running with node one. I'm going to press ctrl c to send a sig int. You can see what happened. I got this text sig in emitted and it seems it happens several times. Okay, so that's how we're going to handle and you can see my server is actually still running and address already in use. So it didn't actually close the server when I did that. Let me kill the process with px. Let me grab that. I think let me see. Hold on. Give me a moment here. I'm grabbing the process number so I can kill it. Okay, so you can in my case, I can just do px. AUX then I pipe it to grab server.js and I can find out that server here is money on process ID 73. I can do kill dash nine, 716 to kill that. Anyway, and I can restart the node one thing. Anyway, before doing that, let's fix this. So let's do the following. Let's say closing connection to the database and then we are going to move line 80 connection dot in here. Then finally we have to close the server to fix that problem where I cannot I have the server still running in the background. So to do that, I need a reference to the server. And we can there's actually a method that you can call to close the server programmatically, we can get that reference from the return value of app.lesson. So let's put that in a variable called server. So app.lesson will return that server. And then we can call that server dot close here. And that will ensure the server is closed after doing after ending the connection to the database. And I don't need this hello here anymore. Let's try it out. Now I'm going to press Ctrl C to send a sig int. And you can see a side closing connection to the database. And I can rerun it and I can still run in the same port, it's not no longer, in no longer kept running the background because of line 78 to close the server. There's also another process called another signal process on sig term, you can also do the same thing. Since I would be using the same code, I can just take this and put that into a function on shutdown, for example, I can put that in an anonymous function or you can I can use the keyword functions up to you, whatever you want. So I'm going to place it here on shutdown. And now I'm shut down. One thing I want to do is I want to say closing the server. Now also when the second term is submitted, I will do the same thing. Okay. So that's it for this lesson. We learned how to handle the signal sig int and sync term so that we can call a callback function, which to properly close the database connection and the server. Usually, for my case, when I'm doing development, I like to kill the server with control C, that would be the sig int signal. That will properly close the connection to the database and close the server. After I'll listen return an object that we can use and call close to and the server. With that, I'll see you in the next lesson.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: