SIGN IN

13. Node.js Lessons. Development, Supervisor

Stay Informed

It's important to keep up
with indusctry - subscribe!

Stay Informed

Looks good!
Please enter correct name
Please enter correct email
Looks good!

node-js-development-Brillmindz

Our goal for this lesson is to improve our Node.js development skills. The first thing that will help us in this task – and we’re going to learn it – is called supervisor. We’ve already created an echo server.

However, the development process contained certain difficulties. Any code change required a server reset. It can be easily explained, since Node.js reads files only one time. Having read the file, it receives a respective module object and further uses it. But what should we do for development?
Of course, there is an option to clean up the internal cache of Node.js, but another approach is used more often. We’re talking about a special module that monitors all files from the directory. As soon as something gets changed within the directory and Node.js gets restarted, one of these modules gets named supervisor. This module contains a console utility that has to be accessible using a system pass.

That’s why let us install it globally:
npm i -g supervisor

Now I launch the following instead of node server.js  -> supervisor server.js

What will it bring to us? Now I will change the file. For example, I will add an empty string to it and save the result.

Have you noticed? Crashing child and a new start. Let us do it again. Delete a string and save the result.

WebStorm saves the result just at a single click on the Save button. Or if it loses the focus. It means, whenever I switch to a browser window, it will immediately save the result of my editing.

Supervisor is constantly monitoring the files, and whenever it sees any change of the files with node or js extension by default, it will restart Node.

Moreover, if this directory has other files and they are changed, supervisor will monitor the directory content, too, and see what files have been changed. As soon as any of them changes, including the directory pass, it will start Node. It is a great feature for developing big multi-level projects.

But what pitfalls does supervisor have? The do exist. The first one you need to know concerns new files. Let us create file.js. Save it.

No reaction follows. It happens because supervisor – at least, its current version – monitors each file individually. It simply doesn’t know that a new file has appeared. But if we add the changed file using a require server, it will monitor exactly these server changes and will restart the program. And after this restart it will supervise the new js file, too. So, if adding a new file gets followed by changes in the existing ones – and this is a common scenario – it means everything is ok. But there may be certain challenges, when we will have to restart the supervisor manually. Otherwise, it won’t see new files automatically.

Another pitfall of the supervisor is that if we install a lot of modules and we’ve got a big node.modules directory – and every module generates its own dependencies – the total number of subdirectory files will be enormous, and the supervisor will devour processing time. This kind of thing is quite evident, if you work on a laptop that will get much hotter. To avoid this situation, it is recommended either to launch the supervisor with an ignor parameter and specify what exactly needs to be (node.modules), or move the node.modules somewhere above because these are external modules. They are rarely changed.

So, we will use the supervisor for our further development.


This lesson’s coding can be found in our repository.

weekly-header-boxes-retina


The materials were borrowed from the following screencast

We are looking forward to meeting you on our website soshace.com

About the author

Stay Informed

It's important to keep up
with indusctry - subscribe!

Stay Informed

Looks good!
Please enter correct name
Please enter correct email
Looks good!

Related articles

20.09.2019

Python range() Explained and Visualized

In this article, we’ll explore the functionality of range() and highlight its use cases — and you’ll know how to use range() in Python>>>

Programming

Understanding Flutter Bloc Pattern

In this article, we'll cover a bit of Reactive Programming, BLOC patterns in Flutter, when and how to use them, as well as look at some alternatives>>>

Programming
17.09.2019

“Learn Python the Hard Way”: a Detailed Book Review

In this article, we'll review a popular book titled "Learn Python the Hard Way" and analyze both its strong and weak sides.>>>

Programming

Sign in

Forgot password?

Do not have account yet? You can create one

Or use a social network account

 

By clicking Sign Up, you agree to our privacy policy

Sign up

Already have an account? Please

Or use a social network account

 

By clicking Sign Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By clicking Sign Up, you agree to our privacy policy