Home > Software design >  Express middleware not capturing request events for socket.io
Express middleware not capturing request events for socket.io

Time:04-18

I'm using Express (v4.17.3), Socket.io, and Node.Js's http module. I'm adding a middleware for express to capture all incoming requests but that's failing.

I'll first show the code I'm using and the output then explain my understanding/expectation of the output (I'm new to Node and all the mentioned libraries so perhaps I'm missing something)

First of all, below is the code I'm referring to. Using Express's middleware I'm trying to capture all the incoming requests and log them, and doing the same for the http on("request"). However, requests going to socket.io aren't captured by the middleware.

// Express
const express = require("express");
const app = express()
// Socket
const server = require('http').createServer(app);
const {Server} = require("socket.io");
const io = new Server(server)

// Want to listen to all incoming requests using the middleware (this doesn't work)
app.use((req,res,next)=>{
    console.log(`Express request = ${req.url}`)
    next()
})

// Listening to all incoming requests (this works)
server.on("request", (req, res)=>{
    console.log(`Http request = ${req.url}`) 
})

server.listen(8080, () => {
    console.log(`Listening on port 8080`)
})

output when I GET /

Express request = /

Http request = /
Http request = /socket.io/socket.io.js
Http request = /socket.io/?EIO=4&transport=polling&t=O0va...
Http request = /socket.io/?EIO=4&transport=polling&t=O0va24A&sid=c...
Http request = /socket.io/?EIO=4&transport=polling&t=O0va24F&sid=c...
Http request = /socket.io/?EIO=4&transport=polling&t=O0va27x&sid=c...

My expected output is to have equal logs for the middleware app.use() and on("request") ("Express request = " & "Http request = ")

My understanding:

1- When I add a middleware for express as in the code below, any incoming requests should be captured here first before going anywhere else. (correct?)

app.use((req,res,next)=>{...})

2- When I'm passing the express app as an argument to http'screateServer, that the express app will be treated as a listener and any request events will be passed to it. (correct?)

const server = require('http').createServer(app);

So if my understanding is correct, why aren't all the requests captured by the request event passed to the middleware as well?

CodePudding user response:

I believe you need to log the messages sent on socket.

io.on('connection', (socket) => { socket.on('chat message', (msg) => { console.log('message: '   msg); }); });

CodePudding user response:

This is normal. Socket.io puts itself in front of express (or any other listener for incoming requests on the http server) no matter when you install it so that it takes the request before Express sees it. Thus, Express (or it's middleware) never see any socket.io connection requests.

Socket.io has its own middleware layer that you can use to participate in the initialization of socket.io requests.

Or, you can register for incoming socket.io connections (to be called after they are already connected) with the io.on('connection', ...) event handler.

When I add a middleware for express as in the code below, any incoming requests should be captured here first before going anywhere else. (correct?)

That is true except for code that registers directly request handlers right on the http server and inserts itself before Express in the listener chain, thus preventing Express from seeing any requests that are destined for socket.io.

When I'm passing the express app as an argument to http'screateServer, that the express app will be treated as a listener and any request events will be passed to it. (correct?)

That is true. But socket.io jumps in front of Express and takes/hides any requests it wants so that Express never sees them.

  • Related