尽管我很了解JavaScript,但我还是不明白Node.js生态系统中的这三个项目到底是做什么的。是不是有点像《Rails’Rack》?有人能解释一下吗?


当前回答

Connect为常见的HTTP服务器功能(如会话管理、身份验证、日志记录等)提供了“更高级别”的api。Express是建立在具有高级功能(类似Sinatra)的Connect之上的。

其他回答

Connect为常见的HTTP服务器功能(如会话管理、身份验证、日志记录等)提供了“更高级别”的api。Express是建立在具有高级功能(类似Sinatra)的Connect之上的。

[更新:从4.0版本开始,Express不再使用Connect。]但是,Express仍然与为Connect编写的中间件兼容。我的原始答案如下。

我很高兴你问了这个问题,因为对于那些使用Node.js的人来说,这绝对是一个常见的困惑点。下面是我最好的解释:

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

这里有一个关于“中间件”含义的具体例子:开箱即用,上面没有一个为您提供静态文件。但只要加上连接。static (Connect附带的中间件),配置为指向一个目录,您的服务器将提供对该目录中的文件的访问。注意,Express也提供Connect的中间件;表达。Static和connect.static是一样的。(直到最近,两者都被称为staticProvider。)

我的印象是,现在大多数“真正的”Node.js应用都是用Express开发的;它添加的功能非常有用,如果你想要,所有低级功能仍然存在。

相关信息,特别是如果您正在使用NTVS与Visual Studio IDE一起工作。NTVS将NodeJS和Express工具,脚手架,项目模板添加到Visual Studio 2012, 2013。

此外,将ExpressJS或Connect作为“WebServer”调用的措辞是不正确的。您可以使用或不使用它们来创建一个基本的WebServer。一个基本的NodeJS程序也可以使用http模块来处理http请求,从而成为一个基本的web服务器。

Node.js本身提供了一个HTTP模块,其createServer方法返回一个对象,您可以使用该对象响应HTTP请求。该对象继承了http。服务器原型。

middleware as the name suggests actually middleware is sit between middle.. middle of what? middle of request and response..how request,response,express server sit in express app in this picture you can see requests are coming from client then the express server server serves those requests.. then lets dig deeper.. actually we can divide this whole express server's whole task in to small seperate tasks like in this way. how middleware sit between request and response small chunk of server parts doing some particular task and passed request to next one.. finally doing all the tasks response has been made.. all middle ware can access request object,response object and next function of request response cycle..

这是一个很好的例子来解释中间件在表达YouTube视频中间件