在Node.js(或其他编程语言)中,是否有用于网络应用程序的“中间件”库?

huangapple go评论109阅读模式
英文:

Are there 'middleware' libs for network based apps in node (or other languages)?

问题

我正在寻找一种在Node.js中编写几个TCP服务器/客户端的方法。我正在尝试弄清楚Node.js生态系统提供了哪些功能,以便使这种开发更加容易。我想到了类似于Java的Netty的一些部分。该项目提供了一种实现协议的方式,使其可以插入网络代码流水线或从实际的TCP连接代码中取出以进行独立测试。

Node.js的HTTP框架通常提供中间件层。用户可以编写拦截请求或响应以隔离功能块的代码,以便实际的请求/响应接口不会发生太大变化。这导致第三方提供了一些用于日志记录、身份验证等功能的代码。

是否存在类似于低级网络代码的解决方案?
(由于我正在测试一些想法,如果有人知道其他非Java/C++/C#语言提供此类功能/库,我也想了解一下)

英文:

I am looking to write a couple of tcp servers/clients in node. I'm trying to figure out what node.js ecosystem provides for making such development easier. I'm thinking of something similar to parts of (java's) netty. That project provides a way to implement the protocol in a way that it can be plugged into the network code pipeline or taken out of actual tcp connection code to be tested independently.

Node's http frameworks often provide a middle-ware layer. Users can write code which intercepts requests or responses to isolate bits of functionality so the actual request/response interface doesn't change much. This results in 3rd party providers of such code which does logging, authentication, etc.

Does something like this exist for lower level networking code?
(since my testing out some ideas, if someone knows of other non-java/c++/c# languages which DO provide such features/libraries, I'd like to read about them as well)

答案1

得分: 3

由于你标记了Elixir,我来回答。

虽然Elixir有一个很好的框架(Plug)可以用来构建类似的HTTP客户端,但我建议考虑使用vegurranch_proxy_protocol库(由Heroku用Erlang编写,但在Elixir中也可以轻松使用)。vegur提供了一个可插拔的中间件层,可以很容易地进行单元测试。我目前在我的公司使用它来构建代理层。

即使没有这些库,使用ranch构建这样的应用也非常简单。Ranch负责监听/接受/读取/写入套接字,你只需在需要的地方插入一个可插拔的流水线,并使用行为(Erlang/Elixir中的接口等效物)进行强制执行。

这类应用程序几乎是Erlang和Elixir的一个亮点,如果你熟悉或对其中任何一种感兴趣,我强烈推荐使用它们。

英文:

Since you tagged Elixir, I'll bite.

While Elixir has a great framework for building something like this for HTTP clients (Plug), an option I'd consider is using the vegur and ranch_proxy_protocol libraries (written in Erlang by Heroku, but easily used in Elixir), vegur offers a pluggable middleware layer which are easily tested in isolation. I'm currently using this for a proxy layer at my company.

Even without those, building an application like this using ranch would be very trivial. Ranch handles listening/accepting/reading/writing from sockets, and you could just insert a pluggable pipeline where needed, enforced using a behaviour (the Erlang/Elixir equivalent of an interface).

These types of applications are pretty much a sweet spot for Erlang and Elixir, I'd highly recommend it if you are familiar or interested in either one.

答案2

得分: 2

我明白了,以下是翻译好的内容:

不确定是否有那么多现成的可用事物,但对于中间件模式本身,有一个名为ware的通用中间件,可在任何JavaScript环境(Node、浏览器)中使用,并且不与HTTP耦合。

英文:

Not sure there are that many ready-made things available, but for the actual middleware pattern itself, there is ware which is generic middleware for use in any JavaScript context (Node, browser) and not coupled to HTTP.

huangapple
  • 本文由 发表于 2016年3月24日 09:14:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/36191601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定