“一切都是中间件"; [英] "Everything is middleware"

查看:101
本文介绍了“一切都是中间件";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Koa JS,用于构建小型Web应用程序的服务器端部分.在观看youtube教程和阅读指南时,我碰到了这样一句话:一切都是中间件(在Koa中)."

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]."

我已经阅读了有关中间件的Wikipedia文章,并用Google搜索了该术语,并且对什么是中间件有了一个大概的了解(介于非常底层的东西和高级编程之间的中间层).但是我不明白在Web应用程序和NodeJS上下文中所谓一切都是中间件"的含义,以及为什么这是相关的.

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.

推荐答案

您可以将Web应用程序请求视为管道.管道是由管道组成的,每次您认为合适时,都可以很容易地向管道中添加新管道.

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.

现在想象一下,添加到管道中的每条管道都能够对流经管道的流体进行特殊处理.例如,如果水是流经管道的流体,则可以添加一个过滤所有灰尘和杂质的管道,然后可以添加将水加热到80°C的管道,然后可以添加向奶粉中添加奶粉的管道.加水,然后添加另一个向其中添加巧克力粉的管道,在管道末端,您会得到巧克力牛奶.

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.

好吧,想象一下同样的事情,但是,顺便说一下,流体就是您的http请求,您可以在添加到管道中的每个管道(即中间件)中对请求执行各种操作,例如下一个管道将获得修改/改进请求的方式.然后,您可以逐步构建http响应,这是您期望在管道另一端出现的内容.

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.

例如,您的请求正文可能已加密,因此您可以在管道中添加解密管道,以便管道中的下一个管道可以处理解密的请求.其他管道可以查找查询参数并将其放入哈希中,其他管道可以查找表单参数并执行相同操作,其他管道可以提取标头值,如何处理cookie?等等,等等,等等.

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.

其中一些管道可用于拒绝请求,例如,在REST API中,您可以在开头添加一个管道来检查请求中发送的API密钥,如果无效,则立即丢弃该请求,并否则将请求发送到管道.

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.

许多Web框架都以这种方式工作,而不仅仅是Koa. Koa由Express的相同创建者开发,后者的工作方式类似,因此很自然地,他们重复使用了Koa中Expeess的最佳创意.但是,早期的框架(如Java Servlet)可以使用称为过滤器的概念以类似的方式工作.因此,这并不是什么新鲜事物,可能只是术语.

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.

这篇关于“一切都是中间件";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆