I'm making a small multiplayer game for the browser using node.js and socket.io. I'm not to familiar with the whole HTTP and HTTPS things.
Currently I'm using a Node.js server for the game on port 3000. With this atempt I am only able to host one server at a time. Either a http or a https server. The problem is, that browser always try to access the http server, but it's insecure.
So my question is: Can I redirect the client to https somehow? Or is there a way to remove the port completly and let it run as a page of my Apache for Ubuntu (Like https://example.com/game)?
The first thing would be to determine if you're in HTTP
Example:
const url = new URL('http://example.com')
console.log(url.protocol) // output: "http:"
Then to forward to HTTPS, just redirect.
res.writeHead(301, { Location: `https://${req?.headers.host}/${path}` });