I'm creating a brutally simple HTTPS server on render.com using Node.js, which upgrades to WebSocket communication. Here's the code:
const https = require('https');
// Create an HTTP server
const server = https.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('okay');
});
server.on('upgrade', (req, socket, head) => {
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n');
socket.pipe(socket); // echo back
});
// Now that server is running
server.listen(443, 'test-srv-676.onrender.com', () => {
// make a request
const options = {
port: 443,
Host: 'test-srv-676.onrender.com',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'websocket',
},
};
const req = http.request(options);
req.end();
req.on('upgrade', (res, socket, upgradeHead) => {
console.log('got upgraded!');
socket.end();
});
});
Instantly, I get an error: the server tries to listen at an IP generated from domain
'test-srv-676.onrender.com', but it is "not available". My HTTPS server setup might be faulty, but I don't know. The error thrown is:
Starting service with 'node server.js'
May 22 09:03:18 AM events.js:353
May 22 09:03:18 AM throw er; // Unhandled 'error' event
May 22 09:03:18 AM ^
May 22 09:03:18 AM
May 22 09:03:18 AM Error: listen EADDRNOTAVAIL: address not available 216.24.57.3:443
May 22 09:03:18 AM at Server.setupListenHandle [as _listen2] (net.js:1301:21)
May 22 09:03:18 AM at listenInCluster (net.js:1366:12)
May 22 09:03:18 AM at doListen (net.js:1503:7)
May 22 09:03:18 AM at processTicksAndRejections (internal/process/task_queues.js:83:21)
May 22 09:03:18 AM Emitted 'error' event on Server instance at:
May 22 09:03:18 AM at emitErrorNT (net.js:1345:8)
May 22 09:03:18 AM at processTicksAndRejections (internal/process/task_queues.js:82:21) {
May 22 09:03:18 AM code: 'EADDRNOTAVAIL',
May 22 09:03:18 AM errno: -99,
May 22 09:03:18 AM syscall: 'listen',
May 22 09:03:18 AM address: '216.24.57.3',
May 22 09:03:18 AM port: 443
May 22 09:03:18 AM }