I'll quickly walk through a couple scenarios which look very similar, but have a much different impact on your code. This may be obvious to you if you're a long time NodeJS developer, but it's something that has tripped me up a few times in the past.
First, let's take a look at the Singleton model and how it works.
The Singleton Model
In the NodeJS Handbook, Fred K. Schott introduces the NodeJS Singleton:"In most languages, sharing an object across your entire application can be a complex process. These Singletons are usually quite complex, and require some advanced modifications to get working. In Node, however, this type of object is the default. Every module that you require is shared across your application, so there’s no need for any special classes or extra code."
In NodeJS we can very quickly and easily create singletons, but it's important to be careful of when to use them, so you don't get yourself in trouble.
Let's take a look at a sample function, which will be our singleton model using a basic Object Oriented Approach. There are other ways to write Singletons as well, but I'm writing the Singleton this way to focus on the small differences between Singletons and Instantiated Objects and how to not get tripped up by them.
Above we have a basic function: MyClass, with a single attribute: name, and single function on that class: askName. Once it's added to the module.exports it is globally available for any of your scripts to require. The important thing to note here is that you are creating a new instantiated object that gets put on the global modules list:
new MyClass("testy1")
. Any script that accesses it references the instantiated object. So in a second script, you could access it like so:
And if you had a third script running in the application you could run it there as well:
Now this is the important thing to remember about Singletons, and the reason you use this pattern: If you change the name in the first script on the same NodeJS process, it will change it in the second script as well. So the following test script which runs both scripts, will output "Hello, my name is Bob" twice:
If you swap the order of these scripts, it will output "testy1" as the name and then "Bob". So just make sure you remember that when you modify values in a Singleton, that it will have that effect on anything else that is accessing it.
When to Use
The Singleton pattern is particularly useful whenever you need to have something store values across all scripts. Think of it as being "super global" to the node process. But that also means that you shouldn't use it if you need to change things between web requests or want to have the object do different things in different areas of your process.
With this pattern, you don't need to create the objects every time. When you require it, you're accessing the created and instantiated object, so you can use it's methods and values right away.
The Object Oriented Model
So what if you don't want to use the Singleton, or it doesn't suit your needs? Well instead of instantiating the object on the module.exports, you can simply pass the constructor back and let the calling script instantiate it.
This fits more along the lines of traditional object oriented programming. Rather than one single global object, you allow the calling script to create each object as needed.
This fits more along the lines of traditional object oriented programming. Rather than one single global object, you allow the calling script to create each object as needed.
The above script is exactly the same as the Singleton model with the exception of line 10. Instead of passing in a new MyClass with a set name, we simply pass in the constructor, the function MyClass. Then in our scripts, when we require it, we must instantiate it, creating the objects with their own values.
There are 2 ways I normally go about instantiating an object from a module. The first is to simply require the class, as seen on line 2, and then instantiate it (line 3) separately or when you need it. Or, if you know that you only need to instantiate it once at the start of the script, say in the case where you have an error library or a debugging library that has settings specific to an individual script, you can instantiate the require, as seen on line 5.
When to Use
The Object Oriented pattern is useful any time you want something to be local to your script or function. One thing to watch out for is the scope of the object and where and when you instantiate a new one. If you're going to be using something for a certain web request, make sure you instantiate it during that web request within the function.One recent 'doh' moment I had was when I instantiated my object globally, but was using it in web requests that came in. So while Node was handling multiple requests, the logic the object was doing was being accessed by 2 different processes and was being corrupted during the request. Moving the instantiation into function where I handled the request solved the issue.
Looking back it seems like a simple mistake, but it threw me for a loop for a while trying to figure out how the data was being corrupted.
There are plenty of other patterns as well, but I wanted to focus on the very small change between a Singleton and a Constructor based pattern that can make all the world of difference in your NodeJS code if you don't realize how it's different.
Hopefully reading this saves you a headache in the future! Thanks and if you enjoyed this article, please follow me on Twitter & Subscribe to the blog! Cheers!