Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Top 30 JavaScript Design Patterns Interview Questions
Next article icon

Top 8 Node.js Design Patterns in 2025

Last Updated : 16 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Node.js, the popular JavaScript runtime, helps developers build complex backend systems. With so many capabilities, it can get quite challenging to work with, and hence, design patterns are used.Design patterns help developers write high-quality, testable, and maintainable code.

Some of the design patterns are built into Node.js, and some can be applied to other programming languages.In this article, we will discuss what the design patterns in Node.js are and the top Node.js design patterns that developers can use in 2025.

Table of Content

  • Top Node.js Design Patterns in 2025
    • 1. Immediately Invoked Function Expressions (IIFE)
    • 2. Module Pattern
    • 3. Event-Driven Pattern
    • 4. Singleton Pattern
    • 5. Factory Pattern
    • 6. Dependency Injection Pattern
    • 7. Middleware Pattern
    • 8. Promise Pattern
  • Conclusion

What are Design Patterns in Node.js?

Design patterns in Node.js are some of the most optimal solutions to common Node.js development problems. They enable developers to write better, more scalable, and more maintainable Node.js code. Developers often use one of the design patterns whenever they are stuck on an issue.

There are plenty of design patterns available for Node.js. However, it is important to note that they should be used judiciously.

Design patterns should not be forcefully applied where they are not required.

Top Node.js Design Patterns in 2025

Nodejs-Design-patterns

There are various design patterns in Node.js, but some of the best ones are mentioned below:-

1. Immediately Invoked Function Expressions (IIFE)

IIFE are functions that are invoked as soon as they are declared. This common design pattern can be used in Node.js for a couple of things:

  • Encapsulation: Code is encapsulated within local scope.
  • Privacy: Variables and functions cannot be used outside the scope.

Consider the code below:

Node
(function (parameter) {   const a = parameter;   const b = 20;   const answer = a * b;   console.log(`The answer is ${answer}`); })(4); 
  • We have defined a function that is immediately invoked by passing “4” as the parameter.
  • It simply multiplies “a” and “b” and assigns the value to “answer” before logging the string on the console.

The output is:

The answer is 80

2. Module Pattern

One of the most fundamental design patterns in Node.js, the module pattern is used to separate and encapsulate some code into different modules. This pattern helps to organize the code and hide the implementation details as well.

For example, look at the code below:

Node
// module.js const firstName = "Mark";  const displayFirstName = () => {   return `Hi, ${firstName}!`; };  const lastName = "Harris";  const displayLastName = () => {   return `Hi, ${lastName}!`; };  module.exports = { lastName, displayLastName }; 

In module.js:

  • We have assigned two different strings to two different variables, “firstName” and “lastName”.
  • There are two functions “displayFirstName” and “displayLastName”.
  • Lastly, we have exported “lastName” and “displayLastName”.
Node
// index.js const modules = require("./module");  console.log(modules.displayLastName()); 

In index.js (above code), we have imported the module.js file, and we are logging out “displayLastName” which outputs:

Hi, Harris!

In the above example, we have used the module pattern that hides the implementation details of the variables and functions defined in module.js. Notice that we can only use “lastName” and “displayLastName” in index.js as we have only exported those two.

This means, we have access to only “lastName” and “displayLastName”. On the other hand, “firstName” and “displayFirstName” are not accessible from index.js.

3. Event-Driven Pattern

The event-driven pattern utilizes the event-driven architecture of Node.js to handle events. For handling events, it uses the EventEmitter class. An event emitter enables developers to raise an event from any part of the application that can be listened to by a listener and an action can be performed.

Look at the code shared below:

Node
const EventEmitter = require("events");  const emitter = new EventEmitter();  emitter.on("someEvent", () => {   console.log("An event just took place!"); });  emitter.emit("someEvent"); 
  • Here, we have imported the events module.
  • We have created an instance of the EventEmitter class.
  • We have registered an event titled “someEvent”, which takes a callback function.
  • Finally, we have triggered the event using the “emit” method. When the event is emitted or triggered, the callback function associated with “someEvent” gets called.

Output on the console:

An event just took place!

The events executed by event emitters are executed synchronously. This pattern helps developers use event-based programming in Node.js.

4. Singleton Pattern

The singleton pattern is one of the most popular design patterns in programming languages. In Node.js, the singleton pattern is used where there is a requirement for only one instance of a class.

Let’s understand this with the code.

Node
class Singleton {   constructor() {     if (!Singleton.instance) {       Singleton.instance = this;     }     return Singleton.instance;   }    displayString() {     console.log("This is a string.");   } }  const firstInstance = new Singleton(); const secondInstance = new Singleton();  console.log(firstInstance === secondInstance); // true  firstInstance.displayString(); 

In the above code:

  • We have created a class titled “Singleton”.
  • Firstly, we have checked if an instance of the class exists, if it does not exist, we are assigning the current instance of the class to “Singleton.instance”. If the instance already exists, we simply return the existing instance. This ensures that all the instances of this class reference the same object.
  • Next, we have defined a method that displays a string on the console.
  • We have created two instances of the “Singleton” class: “firstInstance” and “secondInstance”.
  • We have checked if both instances reference the same object.
  • Lastly, we have called the “displayString” method on “firstInstance”.

Output:

true

This is a string.

5. Factory Pattern

A type of creational design pattern, the factory pattern uses a single object that works as a factory to create new objects. This design pattern hides the implementation logic and enhances flexibility and loose coupling in terms of object creation.

In the code below, we have created a “Motorcycle” class containing the name and the brand.

Node
class Motorcycle {   constructor(name, brand) {     this.name = name;     this.brand = brand;   } }  class MotorcycleFactory {   createMotorcycle(type) {     switch (type) {       case "hunter":         return new Motorcycle("Hunter 350", "Royal Enfield");       case "ronin":         return new Motorcycle("Ronin", "TVS Ronin");       default:         throw new Error("Invalid motorcycle type");     }   } }  const factory = new MotorcycleFactory();  const productA = factory.createMotorcycle("hunter"); const productB = factory.createMotorcycle("ronin");  console.log(productA.name); // Hunter 350 console.log(productB.name); // Ronin 
  • Next, we have created a “MotorcycleFactory” class containing a method that creates a new instance of the “Motorcycle” class based on the type of motorcycle.
  • We have created an instance of the “MotorcycleFactory” class titled “factory”. We use this instance to create other instances of the class.
  • Lastly, we define the two products having different motorcycle types passed on the “createMotorcycle” method.

6. Dependency Injection Pattern

The dependency injection pattern enables classes to be more modular and testable, along with being loosely coupled. It does so by injecting dependencies into other classes instead of creating the dependencies inside the classes.

The code given below has two classes:

  • Display: It has a method called “displayMessage” that simply logs a message.
  • Employee: It has two variables and a method called “show”, which takes in the method of class “Display” and passes the message as a parameter.
Node
class Display {   displayMessage(message) {     console.log(message);   } }  class Employee {   constructor(display, name) {     this.display = display;     this.name = name;   }    show() {     this.display.displayMessage("Employee's name is " + this.name);   } }  const display = new Display(); const user = new Employee(display, "John"); user.show(); // Employee's name is John 
  • Notice that “display” is injected as a dependency by passing it as a parameter and assigning it to “this.display”.
  • The injected “display” is used inside the “show” method, and the “displayMessage” is called.
  • In the later stages, a new instance of class “Display” is created.
  • Along with it, the instance of class Employee is also created, and it passes two parameters inside it. We have passed the “display” instance as a parameter as well, and this is the dependency injection.
  • Finally, the method “show” is called on the “user” instance.

Output:

Employee's name is John

7. Middleware Pattern

Express, a popular Node.js framework has the concept of middlewares, which are very useful in performing certain tasks. Middleware functions are those functions that perform tasks between the request and response of an API call. Middlewares have access to the request and response objects.

This is how we define middleware in Node.js/Express:

Node
const express = require("express"); const app = express();  app.use((req, res, next) => {   console.log("This is a Middleware");   next(); }); 
  • We use the “use()” method to define a middleware, and it has three arguments: req, res, and next.
  • The “next()” function is used to pass the control to the next middleware or a route handler.

Here is a working example of a middleware pattern.

Node
const express = require("express"); const app = express();  app.use((req, res, next) => {   console.log("This is a Middleware");   next(); });  app.get("/", (req, res) => {   res.send("GET request handled!"); });  app.listen(4000, () => {   console.log("listening on port 4000"); }); 

In the above code, when we hit “http://localhost:4000”, on the browser, we see “GET request handled!” displayed on the page. But on the console, we see:

This is a Middleware

This is because:

  • When we hit the route “/”, the control of the Node.js code first passes through the middleware.
  • When it encounters next(), it goes to the next middleware or a route handler, in this case “/”.
  • Finally, the GET request is hit.

8. Promise Pattern

A lot of times, developers need to handle asynchronous operations in Node.js, and that’s where a promise comes in. The promise pattern is extremely powerful in the sense that it helps to execute asynchronous operations in a sequential manner.

Firstly, a promise can be created by instantiating a Promise class, which takes a couple of parameters: “resolve” and “reject”. Look at the code given below to understand.

Node
const myPromise = new Promise((resolve, reject) => {   let x = 5;   // Asynchronous operation   setTimeout(() => {     if (x > 0) {       // If operation is successful       reject("Success");     } else {       // If operation fails       reject("Failure");     }   }, 2000); });  myPromise   .then((res) => console.log(res))   .catch((err) => console.error(err)); 
  • Inside the promise, there is a “setTimeout” function that runs after 2 seconds. If the variable “x” is greater than 0, it resolves the promise; otherwise, it rejects it.
  • We can make use of “then()” and “catch()” methods to consume the promise. If the promise is resolved, the “then()” method handles it.
  • If the promise is rejected, the “catch()” method handles the error.

In the above code, after 2 seconds, the promise gets resolved, and if it is resolved, the control goes to the "then()" method, and “Success” is printed on the console.

Output:

Success

Conclusion

Node.js design patterns enables developers to write highly modular and testable code. In this article, we discussed what the design patterns are in Node.js and some of the best Node.js design patterns. Understanding and using design patterns on the actual code will not only solve a problem but they will enhance the performance as well.


Next Article
Top 30 JavaScript Design Patterns Interview Questions

S

sumsourabh14
Improve
Article Tags :
  • GBlog
  • Web Technologies
  • Node.js
  • nodejs
  • GBlog 2024
  • GBlog 2025

Similar Reads

  • 8 API Design Trends to Watch in 2025
    For a particular platform, the Application Program Interface (API) is particularly a toolkit for creating software applications. The process of developing intentional decisions about how an API will show the data and the functionality to its customers is known as API design. The successful API desig
    8 min read
  • What is Reactor Pattern in Node.js ?
    Reactor Pattern is used to avoid the blocking of the Input/Output operations. It provides us with a handler that is associated with I/O operations. When the I/O requests are to be generated, they get submitted to a demultiplexer, which handles concurrency in avoiding the blocking of the I/O mode and
    3 min read
  • Top 30 JavaScript Design Patterns Interview Questions
    The Design Patterns are basically defined as the reusable and generalized solutions to the common problems that arise during software design and development. Design patterns are not specific to any particular programming language or technology instead, they provide abstract templates or blueprints f
    15+ min read
  • Best Security Practices in Node.js
    The security of an application is extremely important when we build a highly scalable and big project. So in this article, we are going to discuss some of the best practices that we need to follow in Node.js projects so that there are no security issues at a later point of time. In this article, we
    4 min read
  • 10 Reasons To Learn Node.js In 2024
    In the ever-evolving landscape of software development, Node.js stands out as a powerful and versatile tool. Built on Chrome's V8 engine, Node.js enables developers to run JavaScript code on the server side, outside of a web browser. Its unique features and capabilities have made it a game-changer i
    10 min read
  • Top Node Development Trends in 2024
    Node, created by Ryan Dahl in 2009, has emerged as the leading cross-platform, open-source JavaScript runtime, widely adopted by both small and large enterprises for building fast and scalable real-time web applications. In this article, we are going to explore the features of Node and learn about s
    6 min read
  • JavaScript Design Patterns Tutorial
    Design patterns in Javascipt are communicating objects and classes that are customized to solve a general design problem in a particular context. Software design patterns are general, reusable solutions to common problems that arise during the design and development of software. They represent best
    8 min read
  • Top 10 Node Js Developer Skills to Have in 2025
    In today’s world, Node JS is used everywhere. Companies like LinkedIn, PayPal, and Netflix utilize Node JS for their web services. Slack uses Node JS for its backend to handle real-time messaging. It is used for developing RESTful APIs due to its non-blocking I/O model. Uber and eBay have microservi
    8 min read
  • How to handle Child Threads in Node.js ?
    Node.js is a single-threaded language and uses the multiple threads in the background for certain tasks as I/O calls but it does not expose child threads to the developer.But node.js gives us ways to work around if we really need to do some work parallelly to our main single thread process.Child Pro
    2 min read
  • Adapter Method | JavaScript Design Patterns
    Adapter Pattern in JavaScript is a structural design pattern that allows you to make one interface or object work with another that has a different interface. It acts as a bridge, enabling the compatibility of two systems that would not naturally work together. Important Topics for the Adapter Metho
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences