什么是Node.js的Connect,Express和“中间件"? [英] What is Node.js' Connect, Express and "middleware"?

查看:93
本文介绍了什么是Node.js的Connect,Express和“中间件"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管非常了解JavaScript,但我对Node.js生态系统中这三个项目的确切含义感到困惑.就像Rails的架子吗?有人可以解释吗?

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 the http.Server prototype.

连接还提供了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的所有功能, plus 视图呈现和方便的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.staticconnect.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和“中间件"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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