I am writing a server-side node.js application that uses express to handle HTTP requests and I'm adding websockets (socket.io) support. However I don't have access to the code that starts the HTTP server. I am writing an express router and functions to handle various URLs. My code looks like:
var express = require('express'); var router = express.Router(); router.post( myURL, myFunction );
When setting up the router I want to use socket.io to listen for websocket connections as well. I cannot pass express
or express()
into require('socket.io')( xxx )
, I need to pass the http.Server object. But since I didn't start the server, how can I get it?
Express is made on top of the HTTP module. HTTP module provides various tools (functions) to do things for networking like making a server, client, etc. Express along with what HTTP does provide many more functions in order to make development easy.
Express is a web application framework for Node. js that allows you to spin up robust APIs and web servers in a much easier and cleaner way. It is a lightweight package that does not obscure the core Node. js features. In this article, you will install and use Express to build a web server.
Go to the terminal and run the following command. After creation adds key & cert file in your code, and pass the options to the server. const express = require('express'); const https = require('https'); const fs = require('fs'); const port = 3000; var key = fs. readFileSync(__dirname + '/../certs/selfsigned.
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()); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With