From the course: Node.js: Design Patterns
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Singletons in Node - Node.js Tutorial
From the course: Node.js: Design Patterns
Singletons in Node
- [Instructor] In the last lesson we solved the problem of creating multiple instances of the logger class throughout our application by creating a singleton class and then exporting that class to make sure that we only use one instance of the logger. Implementing the get instance method in a singleton class is pretty typical for an object oriented programming language. It's just fine to implement the singleton this way within Node JS. It's also nice because it tells other developers that the logger is a singleton. However, there is a easier and more typical solution for singletons in Node JS, and that's simply to export the instance from the Node JS module. So, within our exercise files under chapter two, lesson three, I'm going to come into my logger.js file and just implement the singleton an easier way by removing the class. Or the need to export a single class and now on line 19 where I export this module,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.