Share
Explore

Service-Oriented Architecture (SOA) with Node.js

Now let's leverage all our work to segue into a lab about writing distributed Service Oriented Architectures with Node.js.
Introductory lecture and some illustrative lab code to show an MVP SOA distributed application

Introductory Lecture on Service-Oriented Architecture (SOA) with Node.js

Lecture Title: Introduction to Service-Oriented Architecture (SOA) with Node.js
Course: CSD-3103 - Full Stack JavaScript

1. Introduction to Service-Oriented Architecture (SOA):

What is SOA?
MVC is a “code-oriented architecture”.
MVC is one very use case of SOA. In MVC the controller is the file in which all the algorithms reside: MONOLITHIC containment means “HIGH COUPLING”.

An ideal software application should obey the following 2 rules:
High Cohesion: 1 Class Does exactly 1 JOB (algorithm).
Low Coupling: Coupling = (Low coupling means I want to Minimize the number of ) Method Calls.

Service-Oriented Architecture (SOA) is the favored emerging a design pattern where services are provided to other components by application components, through a communication protocol over a network.

The basic principles of service-oriented architecture are independent of any vendor, product, or technology.

Key Characteristics of SOA:
Loose Coupling: Services maintain a relationship that minimizes dependencies.
Reusability: Designed to promote reuse of services.
Interoperability: Service Modules communicate with each other by IP address and port and can work together across different platforms and languages.
Composability: Services can be composed to form more complex services.

2. Benefits of SOA:

Scalability: Services can be scaled independently.
Flexibility: Easier to adapt and change individual services without affecting the whole system.
Maintainability: Simplified maintenance due to modular nature.
Development Efficiency: Parallel development is possible as different teams can work on different services.

3. SOA in Modern Distributed Web Applications:

Modern web applications often use SOA principles to create scalable, maintainable, and efficient systems.
Microservices, a variant of SOA, are commonly used to build distributed systems.

4. Node.js and SOA:

Node.js is a powerful platform for building scalable network applications. Its non-blocking I/O model makes it suitable for I/O-heavy operations, which are common in SOA.

Illustrative Lab: Creating an MVP SOA Distributed Application

In this lab, we will build a simple distributed application with Node.js using SOA principles. We will create two services:
User Service: Manages user information.
Joke Service: Provides random jokes.

Lab Setup:

1. Setting Up the User Service:

user-service.js
javascript
Copy code
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3001;

app.use(bodyParser.json());

let users = [
{ id: 1, name: 'John Doe', email: 'john.doe@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane.smith@example.com' }
];

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

app.post('/users', (req, res) => {
const newUser = req.body;
newUser.id = users.length + 1;
users.push(newUser);
res.status(201).json(newUser);
});

app.listen(port, () => {
console.log(`User Service listening at http://localhost:${port}`);
});

2. Setting Up the Joke Service:

joke-service.js
javascript
Copy code
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3002;

app.use(bodyParser.json());

const jokes = [
"A Klingon, a Ferengi, and a Betazoid woman were sitting around in Quark's bar...",
"Q: How many Klingons does it take to change a light bulb? A: None. Klingons aren't afraid of the dark.",
"Klingons do not make software bugs. Their software is perfect from the first compile.",
"Klingon programming does not tolerate error messages."
];

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

app.get('/joke', (req, res) => {
const randomIndex = Math.floor(Math.random() * jokes.length);
const joke = jokes[randomIndex];
res.json({ joke });
});

app.listen(port, () => {
console.log(`Joke Service listening at http://localhost:${port}`);
});

3. Creating the API Gateway:

The API Gateway will act as a single entry point for clients to interact with our services.
api-gateway.js
javascript
Copy code
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;

const userServiceUrl = 'http://localhost:3001';
const jokeServiceUrl = 'http://localhost:3002';

app.get('/users', async (req, res) => {
try {
const response = await axios.get(`${userServiceUrl}/users`);
res.json(response.data);
} catch (error) {
res.status(500).send('Error fetching users');
}
});

app.post('/users', async (req, res) => {
try {
const response = await axios.post(`${userServiceUrl}/users`, req.body);
res.status(201).json(response.data);
} catch (error) {
res.status(500).send('Error creating user');
}
});

app.get('/jokes', async (req, res) => {
try {
const response = await axios.get(`${jokeServiceUrl}/jokes`);
res.json(response.data);
} catch (error) {
res.status(500).send('Error fetching jokes');
}
});

app.get('/joke', async (req, res) => {
try {
const response = await axios.get(`${jokeServiceUrl}/joke`);
res.json(response.data);
} catch (error) {
res.status(500).send('Error fetching joke');
}
});

app.listen(port, () => {
console.log(`API Gateway listening at http://localhost:${port}`);
});

Running the Lab:

Start the User Service:
bash
Copy code
node user-service.js

Start the Joke Service:
bash
Copy code
node joke-service.js

Start the API Gateway:
bash
Copy code
node api-gateway.js

Testing the Services:

Access the user service via the API Gateway:
GET http://localhost:3000/users
POST http://localhost:3000/users (with user data in the body)
Access the joke service via the API Gateway:
GET http://localhost:3000/jokes
GET http://localhost:3000/joke

Conclusion:

This lab demonstrates the basics of building a distributed application using Service-Oriented Architecture with Node.js. You now have a foundational understanding of how to set up and interact with different services via an API Gateway. This architecture is scalable, maintainable, and a fundamental pattern in modern web development.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.