Given an Http.IncomingMessage
created by an http(s).Server
in response to a request what's the correct / recommended way to detect if the request is http vs https?
Some random ideas that I have no idea are correct
Check the port
// seems wrong - might be using a different port
const isHTTPS = req.socket.address().port === 443;
Check if the socket has a cert
// does no cert mean it wasn't https?
const isHTTPS = req.socket.getPeerCertificate &&
request.socket.getPeerCertificate() !== null;
Check if the socket has the function getPeerCertificate
// does no func = !https?
const isHTTPS = req.socket.getPeerCertificate !== undefined;
Check if the socket is a tls.TLSSocket
const tls = require('tls');
const isHTTPS = req.socket instanceof tls.TLSSocket;
other?
req.socket
will have a property called encrypted
set to true
for HTTPS connections.
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