Confusion regarding express server and nodejs http server

谁说胖子不能爱 提交于 2021-02-08 02:00:36

问题


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);
  });
});

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...')
})

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

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?

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


回答1:


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.

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

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.

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}`) })


来源:https://stackoverflow.com/questions/44781368/confusion-regarding-express-server-and-nodejs-http-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!