DEV Community

Sasith Warnaka
Sasith Warnaka

Posted on • Updated on • Originally published at dev.to

Node.js Architectural Patterns with Examples

Exploring Node.js Architectural Patterns with Examples
Node.js, with its non-blocking, event-driven architecture, has become a popular choice for building a wide range of applications. When developing with Node.js, it's essential to choose the right architectural pattern to match your project's requirements. In this article, we'll explore several Node.js architectural patterns and provide examples to illustrate their use.

1. MVC (Model-View-Controller)

The Model-View-Controller (MVC) pattern is a widely-used architectural pattern for web applications. It separates an application into three components:

Model: Handles data and business logic.
View: Handles presentation and user interface.
Controller: Manages the interaction between Model and View.

Here's a simple Node.js MVC example using Express.js:

const express = require('express');
const app = express();

// Model
const items = [];

// View
app.get('/items', (req, res) => {
  res.json(items);
});

// Controller
app.post('/items', (req, res) => {
  const newItem = req.body;
  items.push(newItem);
  res.status(201).json(newItem);
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

2. RESTful API

Node.js is a popular choice for building RESTful APIs. RESTful architecture follows principles such as statelessness and uniform interfaces.

Here's a simple REST API example using Express.js:

const express = require('express');
const app = express();

app.get('/api/books', (req, res) => {
  // Return a list of books
});

app.get('/api/books/:id', (req, res) => {
  // Return details of a specific book
});

app.post('/api/books', (req, res) => {
  // Create a new book
});

app.put('/api/books/:id', (req, res) => {
  // Update a book
});

app.delete('/api/books/:id', (req, res) => {
  // Delete a book
});

app.listen(3000, () => {
  console.log('RESTful API server is running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

3. Microservices

Microservices architecture involves breaking down a complex application into small, independent services. Each service has its own functionality and communicates with others via APIs. Node.js is well-suited for building microservices due to its lightweight nature and scalability.

Here's a simplified example:

// Service 1
const express = require('express');
const app = express();
// Define service 1 routes and functionality

// Service 2
const express2 = require('express');
const app2 = express2();
// Define service 2 routes and functionality

// ...

app.listen(3001, () => {
  console.log('Service 1 is running on port 3001');
});

app2.listen(3002, () => {
  console.log('Service 2 is running on port 3002');
});
Enter fullscreen mode Exit fullscreen mode

4. Real-time Applications

Node.js is an excellent choice for real-time applications that require low-latency communication between the server and clients. Libraries like Socket.io make it easy to implement real-time features.

Here's a basic chat application example:

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIo(server);

io.on('connection', (socket) => {
  console.log('A user connected');

  socket.on('chat message', (message) => {
    io.emit('chat message', message);
  });

  socket.on('disconnect', () => {
    console.log('A user disconnected');
  });
});

server.listen(3000, () => {
  console.log('Chat server is running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

5. Event-Driven Architecture

Node.js's event-driven nature makes it suitable for an Event-Driven Architecture. You can use the EventEmitter module to build systems that respond to events and asynchronous actions.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

myEmitter.on('event', () => {
  console.log('An event occurred!');
});

myEmitter.emit('event');
Enter fullscreen mode Exit fullscreen mode

6. GraphQL

GraphQL is a query language for APIs that allows clients to request exactly the data they need. Node.js can be used to build GraphQL servers, making it suitable for situations where clients have diverse data requirements.

Here's a simplified example using the Apollo Server library:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello, world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`GraphQL server ready at ${url}`);
});
Enter fullscreen mode Exit fullscreen mode

7. Layered Architecture

Similar to MVC, you can organize your Node.js application into layers such as presentation, business logic, and data access. This promotes separation of concerns and maintainability.

8. CQRS (Command Query Responsibility Segregation)

In the CQRS (Command Query Responsibility Segregation) pattern, you separate the reading and writing parts of your application. Node.js can be used to build the APIs for both the command and query sides of your system.

9. Hexagonal Architecture

Hexagonal Architecture emphasizes the separation of concerns and the use of ports and adapters to isolate the core application from external dependencies. Node.js can be used effectively in this pattern.

The choice of architectural pattern depends on your project's specific requirements, scalability needs, and your team's familiarity with the pattern. Often, a combination of these patterns is used within a single application to address different concerns effectively.

Explore these architectural patterns and choose the one that best fits your Node.js project to ensure scalability, maintainability, and performance.

Top comments (5)

Collapse
 
egolegegit profile image
egolegegit

great article! all together in a condensed form

Collapse
 
rjt007 profile image
Rajat Agrawal

Clear and concise.

Collapse
 
souguiroumaima7 profile image
Souguir Oumaima

Good Article !

Collapse
 
ig98 profile image
Ig

This article feels AI generated 🙃 also according to ai content detector

Collapse
 
sasithwarnakafonseka profile image
Sasith Warnaka

Yes, the text is AI-generated because my grammar is not good enough. Because of that, I used AI bot like QuillBot.