I'm putting together a website that serves pages, and also has a chat.
The website is built using Express. For the chat, I'd like to use websockets, and for this I was looking into the node:tls module.
Setup:
// https server
import express from 'express';
import https from 'https';
https.createServer({
key: readFileSync(keyPath),
cert: readFileSync(certPath),
}, app).listen(3000);
...
// tls server
import { createServer } from 'node:tls';
import fs from 'fs';
createServer({
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
}, socket => {
socket.on('data', ...);
}).listen(3001);
Is there a way for this setup to use a single server, and a single port? The https server inherits from the tls server, so I guess it could also work with the websocket.
import { connect } from 'node:tls';
const socket = connect({
host: process.env.RELAY_SERVER_HOST_ORIGIN,
port: 3000,
});
^ The above does not seem to be seen by the https server, even with:
https.createServer({
key: readFileSync(keyPath),
cert: readFileSync(certPath),
}, (req, res) => {
console.log(req); // <--- This is never hit
return app(req, res);
}).listen(3000);
I'm not sure what I am missing here (probably a lot).