问题描述
尽管对 JavaScript 非常了解,但我对 Node.js 生态系统中的这三个项目究竟做了什么感到困惑.是不是类似于 Rails 的 Rack?有人可以解释一下吗?
Despite knowing JavaScript quite well, I'm confused what exactly these three projects in Node.js ecosystem do. Is it something like Rails' Rack? Can someone please explain?
推荐答案
[更新: 从 4.0 版开始,Express 不再使用 Connect.但是,Express 仍然兼容为 Connect 编写的中间件.我的原始答案如下.]
[Update: As of its 4.0 release, Express no longer uses Connect. However, Express is still compatible with middleware written for Connect. My original answer is below.]
很高兴您提出这个问题,因为对于查看 Node.js 的人来说,这绝对是一个常见的困惑点.这是我最好的解释:
I'm glad you asked about this, because it's definitely a common point of confusion for folks looking at Node.js. Here's my best shot at explaining it:
Node.js 本身提供了一个 http模块,其
createServer
方法返回一个可用于响应 HTTP 请求的对象.该对象继承了http.Server
原型.
Node.js itself offers an http module, whose
createServer
method returns an object that you can use to respond to HTTP requests. That object inherits thehttp.Server
prototype.
Connect 还提供了一个 createServer
方法,该方法返回继承 http.Server
扩展版本的对象.Connect 的扩展主要用于方便插入中间件.这就是为什么 Connect 将自己描述为中间件框架",并且经常被类比为 Ruby 的 Rack.
Connect also offers a createServer
method, which returns an object that inherits an extended version of http.Server
. Connect's extensions are mainly there to make it easy to plug in middleware. That's why Connect describes itself as a "middleware framework," and is often analogized to Ruby's Rack.
Express 与 Connect 对 http 模块的作用相同:它提供了一个 createServer
方法扩展了 Connect 的 Server
原型.所以 Connect 的所有功能都在那里,加上视图渲染和一个方便的 DSL 来描述路由.Ruby 的 Sinatra 是一个很好的类比.
Express does to Connect what Connect does to the http module: It offers a createServer
method that extends Connect's Server
prototype. So all of the functionality of Connect is there, plus view rendering and a handy DSL for describing routes. Ruby's Sinatra is a good analogy.
还有其他框架可以进一步扩展 Express!Zappa,例如,它集成了对 CoffeeScript、服务器端 jQuery 和测试的支持.
Then there are other frameworks that go even further and extend Express! Zappa, for instance, which integrates support for CoffeeScript, server-side jQuery, and testing.
以下是中间件"含义的具体示例:开箱即用,以上均不为您提供静态文件.但是只要加入 connect.static
(Connect 附带的中间件),配置为指向一个目录,您的服务器将提供对该目录中文件的访问.请注意,Express 还提供了 Connect 的中间件;express.static
与 connect.static
相同.(直到最近,两者都被称为 staticProvider
.)
Here's a concrete example of what's meant by "middleware": Out of the box, none of the above serves static files for you. But just throw in connect.static
(a middleware that comes with Connect), configured to point to a directory, and your server will provide access to the files in that directory. Note that Express provides Connect's middlewares also; express.static
is the same as connect.static
. (Both were known as staticProvider
until recently.)
我的印象是,现在大多数真正的"Node.js 应用程序都是用 Express 开发的;它添加的功能非常有用,如果您需要,所有较低级别的功能仍然存在.
My impression is that most "real" Node.js apps are being developed with Express these days; the features it adds are extremely useful, and all of the lower-level functionality is still there if you want it.
这篇关于什么是 Node.js 的 Connect、Express 和“中间件"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!