node.js - 有没有办法在 503 错误后自动重启 Nodejs 应用程序?

标签 node.js websocket shared-hosting pusher

我的nodejs应用程序在运行后每隔几周就会收到503资源超出错误,所以我需要通过ssh重新启动它。我想知道是否可以安装一些东西,以便在出现错误或崩溃时自动重新启动它。

我检查了我们的 A2Hosting 服务器在发生错误期间的进程数,但它只是显示 0/50。

我正在实时使用推送器。

const express = require("express");
const router = express.Router();

const Pusher = require("pusher");

var pusher = new Pusher({
  appId: "xxxxxx",
  key: "xxxxxxxxxxxxxxxxxx",
  secret: "xxxxxxxxxxxxxxx",
  cluster: "ap1",
  encrypted: true
});

router.post("/", (req, res) => {
  const newVote = {
    id: req.body.id,
    points: 1
  };

  pusher.trigger("scan", "scan-player", {
    id: req.body.id,
    player_id: req.body.player_id,
    admin_id: req.body.admin_id
  });

  return res.json({
    success: true,
    message: "Scan successful!",
    id: req.body.id
   });
});

module.exports = router;

最佳答案

错误不应导致您的应用程序崩溃,您应该处理它并仅记录错误消息。例如,使用 express error handler ,参见: error handling patterns with express

此外,如果您的应用程序崩溃,某些程序可以自动重新启动。最基本的是 forever :

forever start app.js

它会在您的应用程序崩溃时重新启动。但我认为最好的是 pm2 :

pm2 start app.js

它的作用相同,但有很多附加功能,如负载平衡、部署系统、日志管理

希望有帮助,
最好的问候

关于node.js - 有没有办法在 503 错误后自动重启 Nodejs 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54753873/

相关文章:

node.js - 我的请求正文在 Node API 中显示为空或未定义

node.js - 如何在 Node 迁移中使用 Typescript 作为编译器

javascript - Websockets onMessage 函数 : What happens if the code block within onMessage takes longer to run than interval between two messages?

php - PHP 中非基于浏览器的 websocket 客户端

python-3.x - PY3 : Flask-Socketio Alternative?

c# - 如何在共享主机环境中运行 RavenDB?

javascript - Node.js 执行重放日志系统

mysql - 如何使用nodejs更新mysql中的多个列

angularjs - Angular 2. 在共享主机上运行

symfony - 如何在共享主机上安装Composer以便轻松部署