Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Everything is middleware"

I'm learning Koa JS for building the server side part of a small web applications. Watching youtube tutorials and reading guides, I came across the sentence: "Everything is middleware [in Koa]."

I have read the wikipedia article about middleware and googled the term, and I have a rough understanding of what middleware is (sort of a middle layer between the very low level stuff and high-level programming). But I don't understand what the claim that "Everything is middleware" means in the context of web applications and NodeJS and why this is relevant.

like image 590
tomet Avatar asked Jun 01 '14 13:06

tomet


People also ask

What is middleware?

1 in business computing, middleware is messaging and integration software between applications and services 2 in gaming, middleware is pretty well anything that is provided by a third-party 3 in (some) embedded software systems, middleware provides services that applications use, which are composed out of the... More ...

What is a middleware stack?

For example : The operating system on which your application runs requires an I.P change , you do not have to worry about it in your code , it is the middleware stack on which you can simple update the configuration.

What tasks are commonly implemented by middleware?

Examples of tasks that are commonly implemented by middleware: 1 Gzip response compression. 2 HTTP authentication. 3 Request logging.

Is middleware a homogeneous or heterogeneous system?

With Middleware framework in the middle, the first system will produce X stuff, the system Y and Z would consume those outputs and so on. Greatly example! In this way doesn't it will become a heterogeneous environment? not homogeneous. Middleware is a data (aggregation + transformation) component.


1 Answers

You can think of Web application requests as a pipeline. A pipeline is composed of pipes, and you can very easily add new pipes to your pipeline every time you think that's appropriate.

Now imagine that every pipe you add to your pipeline were capable of doing something special with the fluid flowing through the pipeline. For instance, if water were the fluid flowing through the pipeline, you could add a pipe that filters any dirt and impurities, then you could add a pipe that heats the water to 80C, then you could add a pipe that adds powder milk to the water, and then add another pipe that adds powder chocolate to it and at the end of the pipeline you get chocolate milk.

Well, imagine the same thing, but as you go, the fluid is your http request, and you can do all kinds of things to your request in every pipe (i.e. middleware) that you add to your pipeline, in such a way that the next pipe will get a modified/improved request. And as you go, you can gradually build your http response, which is what you would expect to emerge at the other side of the pipeline.

For instance, the body of your request may come encrypted, so you may add a decryption pipe to your pipeline such that the next pipes in your pipeline can work with a decrypted request. Other pipes can look for query parameters and put them in a hash, other can look for form parameters and do the same, other could extract header values, what about one to deal with cookies?, etc, etc, etc.

So, you can see you can easily add more and more pipes to your pipeline, every one doing something else the previous one did not do. And as you go you improve the request with more and more information and that helps you to eventually build a response to send back to the client.

Some of these pipes can be used to reject the request, for instance, in a REST API you could add a pipe at the beginning that checks the API key sent in the request, and if invalid, immediately discards the request, and otherwise sends the request down the pipeline.

So you can see some pipes work as filters which decide which requests must be handled and which should be discarded or terminated. Other pipes may act as transformers that change the request by adding more data to it or changing data in it and then pass it to the next pipe in the pipeline. Some pipes are routers, that is a pipe with a single entry point but with many exit points; this type of pipe can send the request through a different pipeline depending on its contents (i.e. path, content-type, accepted languages, etc, etc.). Finally, some pipes are terminals, which means when you reach them you are at the end of the pipeline and you are supposed to provide a response there, whether successful or not.

Many web frameworks work this way, not only Koa. Koa is being developed by the same creators of Express, and this latter works in a similar way, so it's only natural they reused the best ideas from Expeess in Koa. However frameworks of earlier days like Java Servlets can work in similar way using a concept called filters. So, this is not new, just probably the terminology.

like image 92
Edwin Dalorzo Avatar answered Sep 28 '22 05:09

Edwin Dalorzo