Socket.io - How do I get a client url request on the server side?

In my app.js (server), I need to know where the request came from (URL).

I am currently passing the URL as a parameter from the client:

socket.emit('request', window.location.href); 

and process it server side

 socket.on('request', function(url){ console.log(url); ... }); 

But this is clearly dangerous and unsafe (clients can send something to the server).

So, I guess ... is it possible to get the URL parameter only on the server side? Maybe from a socket object?

+5
source share
1 answer

To get the connection url

 io.sockets.on('connection', function(socket) { console.log("url: " + socket.handshake.url); }); 

This will return something like: url: /socket.io/1/?t=1407807394827

0
source

Source: https://habr.com/ru/post/1200011/


All Articles