Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Node.js Blueprints

You're reading from   Node.js Blueprints Develop stunning web and desktop applications with the definitive Node.js

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781783287338
Length 268 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Krasimir Stefanov Tsonev Krasimir Stefanov Tsonev
Author Profile Icon Krasimir Stefanov Tsonev
Krasimir Stefanov Tsonev
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Common Programming Paradigms FREE CHAPTER 2. Developing a Basic Site with Node.js and Express 3. Writing a Blog Application with Node.js and AngularJS 4. Developing a Chat with Socket.IO 5. Creating a To-do Application with Backbone.js 6. Using Node.js as a Command-line Tool 7. Showing a Social Feed with Ember.js 8. Developing Web App Workflow with Grunt and Gulp 9. Automate Your Testing with Node.js 10. Writing Flexible and Modular CSS 11. Writing a REST API 12. Developing Desktop Apps with Node.js Index

Exploring middleware architecture

The Node.js framework is based on the middleware architecture. That's because this architecture brings modularity. It's really easy to add or remove functionalities from the system without breaking the application because the different modules do not depend on each other. Imagine that we have several modules that are all stored in an array, and our application starts using them one by one. We are controlling the whole process, that is, the execution continues only if we want it to. The concept is demonstrated in the following diagram:

Exploring middleware architecture

Connect (https://github.com/senchalabs/connect) is one of the first frameworks that implements this pattern. In the context of Node.js, the middleware is a function that accepts the request, response, and the next callbacks. The first two parameters represent the input and output of the middleware. The last one is a way to pass the flow to the next middleware in the list. The following is a short example of this:

var connect = require('connect'),
    http = require('http');
 
var app = connect()
  .use(function(req, res, next) {
    console.log("That's my first middleware");
    next();
  })
  .use(function(req, res, next) {
    console.log("That's my second middleware");
    next();
  })
  .use(function(req, res, next) {
    console.log("end");
    res.end("hello world");
  });
 
http.createServer(app).listen(3000);

The use method of connect accepts middleware. In general, the middleware is just a simple JavaScript function. We can write whatever we want in it. What is important to do at the end is to call the next method. It passes the flow to the next middleware. Often, we will need to transfer data between the middleware. It's a common practice to modify the request or the response objects because they are the input and output of the module. We can attach new properties or functions, and they will be available for the next middleware in the list. As in the following code snippet, we are attaching an object to a data property.

.use(function(req, res, next) {
    req.data = { value: "middleware"};
    next();
})
.use(function(req, res, next) {
    console.log(req.data.value);
})

The request and response objects are identical in every function. Thus, the middleware share the same scope. At the same time, they are completely independent. This pattern provides a really flexible development environment. We can combine modules that do different tasks written by different developers.

You have been reading a chapter from
Node.js Blueprints
Published in: Jun 2014
Publisher:
ISBN-13: 9781783287338
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime