Score:0

Node.js: IP generated from my web hosting domain is "not available" (can't setup a https server)

pg flag

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  }
Score:3
cn flag

You can only bind a server to a IP address that is linked to a local interface on the machine the code is running on.

In this case I assume that your hosting provider is using a form of HTTP/HTTPS proxying where the IP address that the provided hostname resolves to is assigned to a Internet facing machine and then traffic is forwarded to your machine.

Replace 'test-srv-676.onrender.com' with '0.0.0.0' to bind to all addresses on your machine which will then accept connections from the upstream machine.

You will also probably want to be listening on http not https since you don't appear to be providing any TLS certificates locally so I expect the proxy is doing the HTTPS/TLS termination for you.

Score:1
in flag

The server is most probably using a floating IP address that is not directly configured on the server, but instead routed to the actual internal address of your server.

Check the actual IP address of your server or bind to 0.0.0.0 to bind to all interfaces.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.