简体   繁体   中英

Confusion regarding express server and nodejs http server

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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