最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js consistently giving 503 error - Stack Overflow

programmeradmin0浏览0评论

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, - 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

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

# Custom settings are loaded below this line (if any exist)
</VirtualHost>

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. - 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.

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

# Custom settings are loaded below this line (if any exist)
</VirtualHost>
Share Improve this question edited Jun 15, 2016 at 10:40 unicornication asked Jun 15, 2016 at 10:29 unicornicationunicornication 6272 gold badges9 silver badges26 bronze badges 1
  • 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 Commented Jul 18, 2020 at 6:51
Add a ment  | 

2 Answers 2

Reset to default 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/

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./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.

发布评论

评论列表(0)

  1. 暂无评论