25 December 2014

What is Middleware?

Middleware is software that operates between the bounds of two interfaces. It is a layer of abstraction between two other layers. The term 'middleware' is context dependent. It refers to different things depending on the level of abstraction you are speaking of. For example, in standard MVC we could consider a controller a peice of middleware because it it a layer of abstraction which lies between the routes and model.

Middleware simplified

The term middlware may sound intiminating when you hear it for the first time. A lot of terms in programming sound complex but are actually incredibly simple. Wikipedia sums up middleware in a very simple and accurate way by describing it as "software glue".

Real world example:

We want to parse incoming traffic as json before it hits the routes. In other words, we want to put software in the middle between our client and our routes.

We can do that by using body-parser for express.js with the following lines of code: of code:

var express = require('express');
var app = express();
app.use(express.json());

And BAM! That's pretty much it.

If this is your first time learning about middleware then I encourage you to go find ways to use it rather than to read more about it. Reading about something is great to get an awareness but there needs to be a point where that knowledge becomes translated into experience.

And of course no blog post would be complete without an explanatory image: