简体   繁体   English

在socket.io中表达express的目的是什么?

[英]What is the purpose of express in socket.io?

I apologize for this novice question, what is the purpose of express in socket.io and why we need to require express in creating chat application?.can we just use socket.io API to create chat application ? 对于这个新手问题,我深表歉意,在socket.io中表达express的目的是什么,为什么在创建聊天应用程序时需要表达express?我们可以仅使用socket.io API来创建聊天应用程序吗?

Thank you in advance. 先感谢您。

Express is an micro-framework for creating Web Applications with Node.js. Express是用于使用Node.js创建Web应用程序的微框架。 You could think of it as a "Ruby on Rails" extremely lightweight alternative. 您可以将其视为“ Ruby on Rails”极轻量级的替代方案。

You use it with Socket.io because you normally need a base for starting creating an web app. 您将它与Socket.io一起使用是因为通常需要一个基础来开始创建Web应用程序。 Express is usually the common choice, but there are others like Sails.js. Express通常是常见的选择,但是还有其他类似Sails.js的选择。 You could use plain Node.js API to create an application with Socket.io too. 您也可以使用普通的Node.js API通过Socket.io创建应用程序。

A webSocket connection is initiated with an http connection that contains special webSocket headers that is then "upgraded" from http to the webSocket protocol when both sides agree to move to webSocket. 通过包含特殊webSocket标头的http连接来启动webSocket连接,当双方同意移至webSocket时,该HTTPSocket标头将从http“升级”到webSocket协议。 Thus, you need a listening http server in nodejs in order to field webSocket connections. 因此,您需要在nodejs中使用监听的http服务器,以实现webSocket连接。

You don't have to use Express for your web server to use with incoming webSocket connections (you can use just the plain http module in nodejs if you want or anything else that listens for incoming http connections), but socket.io has direct integration with Express such that it takes only a single line of code for socket.io to hook into Express and then be listening for incoming webSocket connections. 您不必使用Web服务器的Express来与传入的WebSocket连接一起使用(如果需要,您可以仅在nodejs中使用纯HTTP模块,也可以使用其他任何监听传入的HTTP连接的方法),但是socket.io具有直接集成使用Express,这样socket.io只需一行代码即可挂接到Express,然后侦听传入的WebSocket连接。

And, as others have said, whenever you have a webServer, you often are also serving web pages and Express is an easy and popular way to server web pages via nodejs so many people using socket.io are already using Express too. 而且,正如其他人所说的,每当您拥有一台webServer时,您也经常在提供网页服务,而Express是一种通过nodejs轻松便捷地通过网页服务器处理网页的方法,因此许多使用socket.io的人也已经在使用Express。

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

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