简体   繁体   English

node.js / Express / Connect / Socket.io的关系

[英]Relationship of node.js / Express / Connect / Socket.io

I'm confused. 我糊涂了。 The main question I have is, when to use pure node.js, when shall I use a framework/MVC like "express" or "connect". 我的主要问题是,何时使用纯node.js,何时我应该使用像“express”或“connect”这样的框架/ MVC。

I know that "express" is just adding a bunch of functionality to "connect", but what is it really good for? 我知道“express”只是为“连接”添加了一堆功能,但它真正有用的是什么? Lets say, I want all my HTTP stuff do against an "Apache" server and only do some partial stuff with node.js (like WebSocket connections, CouchDB, etc.). 可以说,我希望我的所有HTTP内容都针对“Apache”服务器,并且只对node.js做一些部分内容(如WebSocket连接,CouchDB等)。

Would it make sense in this scenario to use "express" or "connect" for some reason? 在这种情况下,出于某种原因使用“express”或“connect”是否有意义?

As far as I know, Socket.IO also handles HTTP requests as a fallback, so would it be just enough to use Socket.IO for those needs ? 据我所知,Socket.IO还处理HTTP请求作为回退,所以它是否足以使用Socket.IO来满足这些需求?

What else is the big advantage using Express/Connect ? 使用Express / Connect还有什么大优势?

Express (or Connect) is an application framework for HTTP web applications. Express(或Connect)是HTTP Web应用程序的应用程序框架。 It's the entry point of your application. 这是您的应用程序的切入点。 It provides some very common functionnalities such as : 它提供了一些非常常见的功能,例如:

  • HTTP Server HTTP服务器
  • URL Routing URL路由
  • Request args 请求args
  • Sessions 会议

It also allows other functionnalities to be easily used (they are called middleware) such as Authentication handling, templating. 它还允许轻松使用其他功能(称为中间件),例如身份验证处理,模板化。

If you just want to implement a pub/sub service through SocketIO, without any pages or other API, just use the Socket.io library (S.io homepage example) : 如果您只想通过SocketIO实现发布/订阅服务,而不使用任何页面或其他API,只需使用Socket.io库(S.io主页示例):

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

If you want to use Socket.io within a more complexe application, serving pages and API, you can (must ?) integrate it with Express (see How To Use ) 如果你想在一个更复杂的应用程序,服务页面和API中使用Socket.io,你可以(必须)将它与Express集成(请参阅如何使用

Hi I have been using Expressjs for some time now and find it particularly useful for the Jade templating engine it provides by default. 嗨,我已经使用Expressjs一段时间了,发现它对默认提供的Jade模板引擎特别有用。 Jade is a new templating language and though I admit it takes some time to get familiar with it, its pretty useful. Jade是一种新的模板语言,虽然我承认它需要一些时间来熟悉它,但它非常有用。 You can write conditionals, mixins, pass variables to your pages, use partials etc. It just makes client side html really easy. 您可以编写条件,混合,传递变量到您的页面,使用部分等。它只是使客户端html非常容易。 Also expressjs sets up your view, javascript, stylesheets directory structure... If followed properly catching responses and rendering html pages are a matter of couple of line of codes. 另外expressjs设置你的视图,javascript,样式表目录结构......如果跟着正确捕获响应和渲染html页面是几行代码的问题。 As discussed above, the http middlewear is a lot easier to implement.. 如上所述,http中间件更容易实现..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM