简体   繁体   English

nodejs的express和socketio的区别

[英]Difference between express and socketio for nodejs

I am new to nodejs programming and going through various js that are being developed for node.我是 nodejs 编程的新手,正在浏览为 node.js 开发的各种 js。 My question is a basic one.我的问题是一个基本问题。 Can someone please explain me the difference between express and socketio.有人可以解释一下express和socketio之间的区别吗?

From what I know, express is a middleware on which we can use template engines like jade to push data to browser.据我所知,express 是一个中间件,我们可以在上面使用 jade 等模板引擎将数据推送到浏览器。 What does socketio do then?那么socketio做什么呢? Is it a transport layer?它是传输层吗? It is confusing to me to understand the difference and why we need express and socket in nodejs apps.理解差异以及为什么我们需要在 nodejs 应用程序中使用 express 和 socket 令我感到困惑。

Express is an application server. Express 是一个应用服务器。 You define routes and write code to generate your application's pages or API responses.您定义路由并编写代码来生成应用程序的页面或 API 响应。 It is basically a port of a ruby project called Sinatra.它基本上是一个名为 Sinatra 的 ruby​​ 项目的端口。 It works for a traditional request/response HTTP model.它适用于传统的请求/响应 HTTP 模型。

Socket.io is there to help you implement a server push model for real time type features such as alerts/notifications, chat, or whatever updates you want to do if you want them to just show up in the browser without waiting for the user to click a "refresh" button or anything like that. Socket.io 可帮助您实现实时类型功能的服务器推送模型,例如警报/通知、聊天或任何您想做的更新,如果您希望它们只显示在浏览器中而无需等待用户单击“刷新”按钮或类似的按钮。

Express http server gives request response model from client to server. Express http 服务器提供从客户端到服务器的请求响应模型。

Socket.io enables bidirectional communication channel between client and server. Socket.io 启用客户端和服务器之间的双向通信通道。

Socket io and express is complete different. socket io和express是完全不同的。 But new beginners get confused because in most of the online tutorials people used application server as express and bidirectional communication channel as Socketio.但是新手会感到困惑,因为在大多数在线教程中,人们使用应用服务器作为像 Socketio 这样的快速双向通信通道。 And they put both the code in a same server.js file.他们将两个代码放在同一个 server.js 文件中。 Lets take below example (Code copied from a famous online tutorial):让我们看下面的例子(从著名的在线教程复制的代码):

const express = require("express");
const app = express();

const port = 3000;
const http = require('http').createServer();

app.use(require('cors')());
const io = require("socket.io")(http, {
   cors: {
       origin: "*",
       methods: ["GET", "POST"]
   }
})
http.listen(port,()=>{
   console.log("server is running on "+port);
})

After reading this code a new node learner will easily get confused.阅读此代码后,新的节点学习者很容易感到困惑。 So It is not required to put both together.所以不需要把两者放在一起。 for example just remove the express codes from the above code example and still the socketio server will run perfectly.例如,只需从上面的代码示例中删除 express 代码,socketio 服务器仍然可以完美运行。

const port = 3000;
const http = require('http').createServer();

const io = require("socket.io")(http, {
   cors: {
       origin: "*",
       methods: ["GET", "POST"]
   }
})
http.listen(port,()=>{
   console.log("server is running on "+port);
})

I do not use express.我不使用快递。 I personally like Apache as my application server.我个人喜欢 Apache 作为我的应用程序服务器。 So you can use any application server separately which will handle your static requests and work as web server.因此,您可以单独使用任何应用程序服务器来处理您的静态请求并作为 Web 服务器工作。

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

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