Which Route Leads To The Middle? 
Published: 2020-07-24 13:33:41


*Exploring routes and middleware functions*

#Introduction

The internet is an environment built on the to and fro movement of words describing persons, places, or things in the world we live in. The seemingly simple task of listening for and responding to those words accordingly can be a daunting duty all by itself. Luckily, we have some key phrases, that move along predetermined paths, being listened for by particular entities, that respond to the keywords, making our online conversations flow smoothly without ever stopping our rants or raves. In this blog, we are going to introduce and explain some of the key players in that data exchange and how they allow us to keep the words flowing as seamlessly as possible.



##What path are my words walking?

The path that I am referring to is generally known as a `route` in computer science. The term ‘route’ basically refers to the entire pathway our words or data must travel in order to reach intended destinations in a network.  The ‘route’ encompasses all the stops and tolls along the way to that destination including all players such as a computer, router, server, or database. More specifically, `routing` or determining the route data will travel and in what way it will be processed, is directly related to the choice of words included in your data package. Remember those keywords I mentioned before, well here they come again, but this time let’s title them requests, or, methods, or verbs. Even though all three choices are interchangeable for the sake of clarity in the remainder of this introduction they will be called verbs.        


![Alt Text](https://logrhythm.com/wp-content/uploads/2020/02/monitoring-ospf-routing-protocols-image-1.png)



## What keywords should I listen for?


The aforementioned verbs, or specifically HTTP Request methods, are one of the key parts of the requests for information that are sent out along these routes. The request and subsequent response have a particular format that consists of other key portions (headers, data types, data amounts, cookies, etc.) but is not in our focus for today. Each of these HTTP verbs will enact a certain protocol to be performed on the data being sent to an intermediate (player along the route) and then elicit a particular response back to the client or entity having sent the request. HTTP stands for Hypertext Transfer Protocol and the verbs that you choose to use will trigger one of these predetermined transfer protocols. I thought that was an interesting little tidbit for you to start making a full picture with because it helped me. Anyway, there is no limit to the number of verbs that can be defined, meaning in the future our intermediates can be programmed with far more response protocols, but there are 5 that are more commonly used than others: GET, POST, PUT, PATCH, & DELETE.           


![Alt Text](https://computing.dcu.ie/~humphrys/Notes/Networks/tanenbaum/7-41.jpg)


## Who’s listening?

Now that we know what words are being listened for, we need to highlight” who” is listening, and that brings us to those intermediates I mentioned along our route. The specific player I am going to refer to is middleware. Middleware is a bridge between the operating system (“OS” => your computer’s software) your machine is using and the applications (the particular programs being run on the “OS”) that are being run on it. Middleware functions are hidden or abstracted from the user but allow for the coupling of multiple applications enabling control of information and transfer between them. The bridge created by middleware allows applications to receive services and have certain capabilities that are not offered by the operating system of your computer, such as messaging, API management, and authentication. All middleware performs a communicative role between applications and the OS but the specific type will be solely dependent upon the tasks you are performing and the information you want to receive. So let’s take a look at a few basic examples of middleware in action. 


``` express.js
var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000)
```
In the above example, we are utilizing middleware functionality provided by the express.js framework. The express framework is imported into our project via the require() function and a new instance of the express framework is then instantiated allowing us access to the frameworks methods. The .get() method from express.js allows us to handle requests for information to a certain endpoint, file or server location, etc. The point we are requesting information from is our first input parameter `’/’` while the second is a callback giving our function access to the request and response objects that will contain our data. Res.send() is a method that allows for response in the form a string to sent to our application and displayed on the DOM. The final component of our example is the app.listen() method which binds our application, to listen for connections on a certain host port.  



#Conclusion 

_Topic Recap:_ 
* We defined a ‘route’
* We highlighted HTTP Request Methods
* We briefly introduced Middleware  
* We viewed a basic example of middleware in use 


Now that you have been exposed to what a route is, what is sent along with them, and who interacts with the packages sent along each, you can be better identify the bridging elements between your front and backend. For the newbie like me, the ability to read and identify particular parts of the code in projects and documentation will greatly enhance your ability to recognize implementation patterns and employ them on your own in the future.     


####Happy Coding!!!  

   
Sources:
* ComputerHope.com (https://www.computerhope.com/jargon/r/route.htm)
* Express Doc (https://expressjs.com/en/starter/basic-routing.html)
* Wikipedia (https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods)
* Microsoft Azure (https://azure.microsoft.com/en-in/overview/what-is-middleware/)