3

I just installed Node.js, got it working for a while, then tried to run a standard Hello World program.

var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

I then launched the domain I set up for node, http://node.foobar.com - I successfully got it to print Hello World when I did the same on my domain last week. However now, I keep getting 503 Service Temporarily Unavailable error.

This happened last week too, after I closed the server, edited the code, then reran the server, it would 503 unless I waited a few minutes before running again. However, waiting makes no difference this time.

Apache config for domain:

<VirtualHost *:80>
ServerAdmin zadmin@localhost
DocumentRoot "/var/zpanel/hostdata/zadmin/public_html/node"
ServerName node.foobar.com

ProxyRequests on
ProxyPass / http://localhost:3102/

# Custom settings are loaded below this line (if any exist)
</VirtualHost>
unicornication
  • 635
  • 2
  • 7
  • 26
  • I know this has been solved before, whoever one root cause of 503s is also the NodeJS code itself. If there is an error in the Node.JS code, your webpage will happily throw a 503 error. **This is just for any passers-by who encountered this issue with the same code, but have a different problem. – TORUS Jul 18 '20 at 06:51

2 Answers2

1

How do you set up the domain?

Did you use another HTTP server as a proxy?

Please check if node is running, and if yes check your HTTP server is too.

Maybe your node crashed or is just not running.

Another test is to connect your node directly:

http://127.0.0.1:8081/

Mario Santini
  • 2,905
  • 2
  • 20
  • 27
  • I am running it on a VPS Unix server and cannot directly access 127.0.0.1:8081. The domain is set up using an Apache VirtualHost as shown in the edit. I did `ps aux | grep node` and see node running. – unicornication Jun 15 '16 at 10:39
  • Actually you pointed me in the right direction, thank you! My VHost setup was using the port `3102`. – unicornication Jun 15 '16 at 10:42
  • 1
    So the issue was that the Apache HTTP server was trying to access the node through the wrong port and results as a 503 error. Right? – Mario Santini Jun 15 '16 at 11:50
0

I've been having the same issue. We're using the (no-longer supported) "node-ews" library to load emails/attachments from our Exchange server, and it regularly throws 503 errors.

My colleagues have told me, meh, this is expected behavior with micro-services (really?) and that I just need to use a library to add some resilience, eg.

https://github.com/resilient-http/resilient.js/#how-does-it-work

My solution was to actually call the Exchange endpoint, and if it's a 503 error, log it (and show how many retries we've done), wait for a second, then try again. This did actually solve the issue for me.

  var EXCHANGE_SLEEP_TIME = 1000;
  var EXCHANGE_MAX_RETRIES = 20;
  var retryNumber = 0;
  var bSuccess = false;
  do {
      //  Attempt to update the categories on this email ("UpdateItem" function), but be careful of 503 errors.
      await ews.run(ewsUpdateFunction, updateArgs)
          .then(result => {
              //  Do something with the result
              bSuccess = true;
          })
          .catch(err => {
              var statusCode = (err.response) == null ? -1 : err.response.statusCode;
              var errorString = `setEmailCategory, attempt: ${++retryNumber}, statusCode: ${statusCode}`;
              if (statusCode != 503) 
                  errorString += `, exception: ${JSON.stringify(err)}`;
              console.log(errorString);
              this.sleep(EXCHANGE_SLEEP_TIME);
          });
  } while (retryNumber < EXCHANGE_MAX_RETRIES && !bSuccess);

  . . . 

  public sleep(ms: number) {
      //  Dumb function, to make the code go to sleep for XX milliseconds
      return new Promise( resolve => setTimeout(resolve, ms) );
  }

Yeah, it's ugly, but frighteningly, it works quite well.

Obviously, the "real" version logs the messages to a Logging service, rather than the console.

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159