关于Express Server和Node.js HTTP Server的困惑 [英] Confusion regarding express server and nodejs http server

查看:44
本文介绍了关于Express Server和Node.js HTTP Server的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习redis,并且在文档中他们具有以下服务器设置:

I am learning redis and in the docs they have the following server setup:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(80);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

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

您可以看到他们正在第一行创建快递服务器,这是我通常要做的,但是随后他们将快递传递给server = require('http'),我猜这是一个http的节点模块服务器.

As you can see they are creating the express server on the first line which is what I usually do, but then they pass express to server = require('http') which I'm guessing is a node module for an http server.

我通常在这里做这样的事情:

Where I'm usually doing something like this:

const express = require('express')
const routes = require('./routes')

api.use('/', routes)

// Fireup up API server
api.listen(9090, () => {
  debug('API server listening on port 9090...')
})

为什么在第一个示例中将express传递给nodejs http模块?

Why in the first example express is being passed to the nodejs http module?

编辑我知道 var server = require('http').Server(app)这一行是这样,因此我们稍后可以使用 var io = require('socket)将服务器传递给socket.io.io')(服务器); 我不明白的是:"http"模块是什么?它从何而来?与快递服务器有何不同?

EDIT I understand that the line var server = require('http').Server(app) is so we can later pass the server to socket.io using var io = require('socket.io')(server); What I don't understand is: what is that 'http' module? where does it come from? how is it different from the express server?

我提供的第二个示例来自使用api前缀进行调用的api后端服务器,这就是为什么它具有'/api'的原因,我仅将其包括在内以演示我通常如何设置服务器并显示我在此过程中未使用 require('http').Server(app); .

My second example I've supplied was from an api backend server that uses the api prefix for calls and that's why it had the '/api', I've only included it to demonstrate how I'm usually setting up a server and to show that I'm not using require('http').Server(app); in the process.

Edit2 基于答案和评论的其他问题.为什么我们需要额外的步骤将服务器传递给socket.io,而不仅仅是直接传递快递服务器?

Edit2 An additional question based on the answer and comments. why do we need the extra step to pass the sever to the socket.io and not just pass the express server directly?

推荐答案

第一个是将Express应用程序安装到新的HTTP Server实例,该实例也正在使用Socket.io侦听套接字连接.这是创建运行Socket.io的Express Server的方法之一.

The first one is mounting the Express application to a new HTTP Server Instance that is also listening with Socket.io for socket connections. This is one of the ways you can create an Express Server that has Socket.io running with it.

这是 socket.io docs 具有与示例代码相同的Express和HTTP模块用法.

Here's the socket.io docs with the same Express and HTTP module usage as the example code.

HTTP模块是该模块的一部分节点核心API,因此它只是节点的一部分.您可以在节点应用程序中随时使用它.Express是使用 http 模块构建的,并包装了 http.Server .因此HTTP和Express的不同之处在于Express建立在 http.Server 类的基础上,并添加了诸如中间件,视图引擎等之类的东西.

The HTTP module is part of the Node Core API, so its just part of node. You can require it whenever within a node application. Express is built using the http module and wraps http.Server. So HTTP and Express are different in that Express builds upon the http.Server class and adds things like middleware, view engines, etc.

如果不需要 socket.io 片段,那么就可以摆脱它,就像在您的第二个代码示例中一样.但是,不确定该代码段中 api 表达实例的来源.

If you don't need the socket.io piece then you can just get rid of that, just like in your second code example. However, not sure where the api express instance came from in that snippet.

const express = require('express')
const routes = require('./routes')
const server = express()
const port = process.env.PORT || 1337

server.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

server.use('/api', routes)
server.listen(port, () => { console.log(`Listening on ${port}`) })

这篇关于关于Express Server和Node.js HTTP Server的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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