How do I get the http.server from the express app?
You can write a simple wrapper to either instantiate or get the Socket IO instance in your Express app, by putting the following in some file:
//Dependencies
const socketIo = require('socket.io');
//Placeholder for instance
let io;
//Export handler to instantiate or get instance
module.exports = function(server) {
if (server) {
io = socketIo(server);
}
return io;
};
Then, you can instantiate this instance the usual way when setting up Express:
const app = require('express')();
const server = require('http').Server(app);
const io = require('./path-to-your-file')(server);
And when you need it later in your routes or code elsewhere, you can get it as follows:
const io = require('./path-to-your-file')();
This is of course a simplified example, but you can modify the logic to suit your needs.
You can also use the following code to accomplish the same thing.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
For more information, I sugges that you refer to this link
As far as I know, the Express app
object does not know the server object. The way things work in Express, the app
object is given to the server object, not the other way around. In fact a single app
object can even be used with more than one server (sometimes done for an http and https server).
You can get access to the server object from within a request handler with req.connection.server
, but that comes from the server as part of the context with a request, that's not something the app
object itself knows.
So, if you want access to the server object for use with socket.io at initialization time, you will have to capture the server object into a variable where it is created.
The code you show in your question does not create or start a server. The usual two ways to start a server for use with Express are this:
var express = require('express');
var app = express();
// Express creates a server for you and starts it
var server = app.listen(80);
Or, you create the server yourself and pass express to it as the handler:
var express = require('express');
var app = express();
// you explicitly create the http server
var server = require('http').createServer(app);
server.listen(80);
In each of these cases, you end up with the server object in a variable and can then use that with socket.io.
You can get access to the server from inside an Express route handler within the processing of a request from either the req
or res
object that are passed to a request handler.
res.connection.server
req.connection.server
The server is returned when you call app.listen()
. For example:
const server = app.listen(process.env.NODE_PORT, () => {
console.log('Listening', server.address());
});