Skip to content

Commit 35d02d3

Browse files
committed
Microservices Architecture
1 parent 7b9e312 commit 35d02d3

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3133,7 +3133,93 @@ The `jwt.sign()` method takes a payload and the secret key defined in `config.js
31333133
<b><a href="#">↥ back to top</a></b>
31343134
</div>
31353135

3136-
#### 73Q. ***How to build a microservices architecture with Node.js?***
3136+
## 73Q. ***How to build a microservices architecture with Node.js?***
3137+
3138+
**Microservices**
3139+
3140+
Microservices are a style of **service-oriented architecture** (SOA) where the app is structured on an assembly of interconnected services. With microservices, the application architecture is built with lightweight protocols. The services are finely seeded in the architecture. Microservices disintegrate the app into smaller services and enable improved modularity.
3141+
3142+
<p align="center">
3143+
<img src="assets/monolithic-and-microservices-architecture.jpg" alt="Microservices" width="600px" />
3144+
</p>
3145+
3146+
There are few things worth emphasizing about the superiority of microservices, and distributed systems generally, over monolithic architecture:
3147+
3148+
* Modularity — responsibility for specific operations is assigned to separate pieces of the application
3149+
* Uniformity — microservices interfaces (API endpoints) consist of a base URI identifying a data object and standard HTTP methods (GET, POST, PUT, PATCH and DELETE) used to manipulate the object
3150+
* Robustness — component failures cause only the absence or reduction of a specific unit of functionality
3151+
* Maintainability — system components can be modified and deployed independently
3152+
* Scalability — instances of a service can be added or removed to respond to changes in demand.
3153+
* Availability — new features can be added to the system while maintaining 100% availability.
3154+
* Testability — new solutions can be tested directly in the production environment by implementing them for restricted segments of users to see how they behave in real life.
3155+
3156+
**Creating Microservices with Node.js**
3157+
3158+
**Step 01: Creating a Server to Accept Requests**
3159+
3160+
This file is creating our server and assigns routes to process all requests.
3161+
3162+
```js
3163+
// server.js
3164+
3165+
const express = require('express')
3166+
const app = express();
3167+
const port = process.env.PORT || 3000;
3168+
3169+
const routes = require('./api/routes');
3170+
routes(app);
3171+
app.listen(port, function() {
3172+
console.log('Server started on port: ' + port);
3173+
});
3174+
```
3175+
3176+
**Step 02: Defining the routes**
3177+
3178+
The next step is to define the routes for the microservices and then assign each to a target in the controller. We have two endpoints. One endpoint called "about" that returns information about the application. And a "distance" endpoint that includes two path parameters, both Zip Codes of the Lego store. This endpoint returns the distance, in miles, between these two Zip Codes.
3179+
3180+
```js
3181+
const controller = require('./controller');
3182+
3183+
module.exports = function(app) {
3184+
app.route('/about')
3185+
.get(controller.about);
3186+
app.route('/distance/:zipcode1/:zipcode2')
3187+
.get(controller.getDistance);
3188+
};
3189+
```
3190+
3191+
**Step 03: Adding Controller Logic**
3192+
3193+
Within the controller file, we are going to create a controller object with two properties. Those properties are the functions to handle the requests we defined in the routes module.
3194+
3195+
```js
3196+
var properties = require('../package.json')
3197+
var distance = require('../service/distance');
3198+
3199+
var controllers = {
3200+
about: function(req, res) {
3201+
var aboutInfo = {
3202+
name: properties.name,
3203+
version: properties.version
3204+
}
3205+
res.json(aboutInfo);
3206+
},
3207+
getDistance: function(req, res) {
3208+
distance.find(req, res, function(err, dist) {
3209+
if (err)
3210+
res.send(err);
3211+
res.json(dist);
3212+
});
3213+
},
3214+
};
3215+
3216+
module.exports = controllers;
3217+
```
3218+
3219+
<div align="right">
3220+
<b><a href="#">↥ back to top</a></b>
3221+
</div>
3222+
31373223
#### 74Q. ***How to use Q promise in Node.js?***
31383224
#### 75Q. ***How to use locale (i18n) in Node.js?***
31393225
#### 76Q. ***How to implement Memcached in Node.js?***
Loading

0 commit comments

Comments
 (0)